Hey, I'm stuck on how to add Wrapping<> support correctly to a custom math struct.
Seems like for all native int types, the Wrapping implementations reside in library/core/src/num/wrapping.rs, and I can't work out if it is even possible to add similar semantics to a custom struct outside of there.
This playground is as far as i've gotten. I can successfully, but uselessly, implement an Add operand that looks like T + Wrapping<T> = Wrapping<T>
, but for builtin integers the operand is Wrapping<T> + Wrapping<T> = Wrapping<T>
I.e. i can do this:
impl core::ops::Add<Wrapping<Foo>> for Foo
But i obviously can't do this:
impl core::ops::Add<Wrapping<Foo>> for Wrapping<Foo>
Not sure if there's any trait bounds or something that i can use to make things click ?
It's actually not so relevant for Add / Sub, because those already have usable wrapping_add / wrapping_sub
, but for AddAssign an equivalent wrapping_add_assign
doesn't exist, so the only way for client code to do wrapping add-assign is to go through Wrapping class.