

Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}Assign<$t> for Wrapping<$...
source link: https://github.com/rust-lang/rust/pull/93208
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

I feel a bit conflicted about these. I agree Wrapping
can be annoying to work with, and this is a big improvement to that. But this also adds some inconcistencies that we might not be able to fix:
- This adds
Add<i32> for Wrapping<i32>
(etc.), but do we also wantAdd<Wrapping<i32>> for i32
? Havingw + 1
work but not1 + w
seems bad. But adding even more implementations oni32
doesn't seem great either. - Should we also add
Add<&i32> for Wrapping<i32>
(etc.), just like we haveAdd<&Wrapping> for Wrapping
? It would be consistent, but it would technically be a breaking change sinceWrapping(1) + &Default::default()
would no longer compile. - How about PartialEq/PartialOrd? We can't add those as it'd be a breaking change, but having
w + 1
work, while still requiring an explicitw == Wrapping(1)
also doesn't feel great. - What if someone wants a non-wrapping result type? E.g. they write
let b: i32 = w + 1;
and forget the.0
inw.0
to get the non-wrapping type. Today, they get an error that they can't addi32
toWrapping<i32>
and they need to pick either the wrapping behaviour or the non-wrapping behaviour explicitly, by wrapping the1
inWrapping(1)
or by unwrapping thew
with.0
. But with this change, the addition would happen in a wrapping way, and then give the an error about assigning aWrapping<i32>
to ai32
. Or not give any error if: i32
wasn't explicit.
This makes me think it might be better if we only do this for the *Assign
traits. wrapping_i32 += 1;
clearly follows the behaviour of the wrapping_i32
variable, and some_i32 += Wrapping(1);
won't compile.
These are just some initial thoughts after thinking about this for a few minutes. I'd love to hear some input from the rest of @rust-lang/libs-api.
This means - if I understood this correctly - the new impls have to be stabilized instantly. Which in turn means, this PR has to kick of an FCP on the tracking issue as well?
In such cases we usually do the FCP on the PR itself.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK