I shared this point of view until I tried to convert Rust-Crypto to use Wrapping - which I why I didn’t contribute to the discussion’s regarding creating Wrapping: I agreed, so, I had nothing to add. Doing the work to update Rust-Crypto, however, changed my opinion. It took a while - I tried very hard to make Wrapping work and I couldn’t. A big part of it not working were all of the ergonomic issues that have been described here. Fixing those ergonomic issues would go a long way to making Wrapping more usable, although, its not clear to me that all of the issues are fixable. Even with all of the ergonomic issues fixed, though, I’m still uncomfortable with using a newtype to change the fundamental behavior of “+”. I feel like having two variables next to each other both being added, you would expect to get the same behavior. However, if one of them is a Wrapping and the other is a bare type, you won’t. I think a dedicated operator for wrapping math makes it much clearer what is going on.
Lets say we fixed all of the ergonomic problems and also that we had wrapping ops like Swift - ie: “&+” means “wrapping add”. In that case, if Wrapping were defined to only permit the wrapping ops - that would be pretty interesting. In that case, this code would be fine:
let a = Wrapping(0u32);
a &+= 4;
But this would be an error
let a = Wrapping(0u32);
a += 4; // Error - trying to use non-wrapping ops on a Wrapping type
What I’m concerned about, without dedicated operations, is code like this:
b += 4;
Is that wrapping or non-wrapping? I can’t tell without going to track down the (possibly type-inferred) declaration. So, Wrapping to make sure that you use the right operations sounds pretty interesting to me. Outside of the existing ergonomic issues, though, what I’m also concerned about is how Wrapping changes the operation being done without making it clear at the place where the operation is used.