Why there is no `saturating_sub()` method on `NonZeroU[SIZE]` types

Well, the original purpose of NonZeroU32 is to be "u32 but with a niche at zero". It didn't even have any of these math-related functions back when it was stabilized (see https://doc.rust-lang.org/1.35.0/std/num/struct.NonZeroU32.html for example), because it wasn't added to be an integer-like type.

Making its methods and trait impls only exist if they still do the same thing as on u32 is helpful for things like https://github.com/bevyengine/bevy/pull/9907 -- it means you can change the type and look at the compiler errors to see what you have to think about. If you were only using things like checked_add and | that still exist, you're fine.


EDIT later: said otherwise, the reason that NonZero::checked_add and such is to reduce unsafe (compared to .get().checked_add), not to make it act like a modular integer.

4 Likes