std::num::Wrapping shift ops take usize rhs

Is there a reason why shifting operations implemented for std::num::Wrapping take usize as an rhs operand instead of u32?

This is inconsistent with for example checked shift operations on primitives which take u32 as the number of bits to shift. Also, methods to count the number of bits such as leading_zeros return u32.

However, I don't see how this can be fixed, even if it is not desirable, because of backwards compatibility. If for example a u32 implementation is added without removing the usize implementation, wrap << 2 would not work anymore because 2 would now be inferred as i32. And even adding an i32 implementation would not solve everything, as then (wrap << 2).0 would give an error that the compiler cannot infer the type of (wrap << 2).

5 Likes