On Casts and Checked-Overflow

I think when not otherwise specified, it should always be the logical content that is considered, and not the representation. So a cast should be valid if and only if the casted value is within the range of the casted-to type. (This is your “Strict Range”, I think.) I think this is the spirit of our overflow-handling design (why should -1i8 as u8 be valid and not 0u8 - 1?), as well as of some other ideas for improved ergonomics.

Last I saw, the WrappingOps trait had grown, or was intended to grow, methods such as wrapping_as_u8(), wrapping_as_i8(), wrapping_as_u16(), and so on for each primitive type. I think these are what should be used when anything other than “Strict Range” semantics is desired. (Whether there’s a way to make these less monomorphic and more ergonomic, e.g. some kind of wrapping_as() that’s generic over the target type, is an independent question, I think. Also perhaps we could make foo as Wrapping<u8> where foo: Wrapping<i8> work in the future, having the old semantics of as on the primitive types.)

1 Like