phimuemue/rs_as_num

This approach doesn't seem to work converting u64 -> i64

Closed this issue · 1 comments

The is_safely_convertible in impl_unsigned_to_signed_internal takes the u64 as LargestSignedType (i64), which will fail to retain the value if it exceeds i64::MAX.

It should return false if the u64 value exceeds i64::MAX, not fail (allow over/underflow, in release compilations) or panic (in debug compilations). I'm not certain how to achieve that.

I think we can solve this by casting to LargestUnsignedType instead of LargestSignedType.

  • self as LargestUnsignedType works since self is unsigned.
  • <$dest as SignedInt>::max() as LargestUnsignedType works since we assume that LargestUnsignedType has at least as many bytes as $dest ($dest is signed)