Trait num_conv::CastSigned
source · pub trait CastSigned: Integer {
type Signed;
// Required method
fn cast_signed(self) -> Self::Signed;
}
Expand description
Cast to a signed integer of the same size.
This trait is implemented for all integers. Unsigned to signed casts are equivalent to
0.wrapping_add_signed(value)
, while signed to signed casts are an identity conversion.
assert_eq!(u8::MAX.cast_signed(), -1_i8);
assert_eq!(u16::MAX.cast_signed(), -1_i16);
assert_eq!(u32::MAX.cast_signed(), -1_i32);
assert_eq!(u64::MAX.cast_signed(), -1_i64);
assert_eq!(u128::MAX.cast_signed(), -1_i128);
assert_eq!(usize::MAX.cast_signed(), -1_isize);
assert_eq!(0_i8.cast_signed(), 0_i8);
assert_eq!(0_i16.cast_signed(), 0_i16);
assert_eq!(0_i32.cast_signed(), 0_i32);
assert_eq!(0_i64.cast_signed(), 0_i64);
assert_eq!(0_i128.cast_signed(), 0_i128);
assert_eq!(0_isize.cast_signed(), 0_isize);
Required Associated Types§
Required Methods§
sourcefn cast_signed(self) -> Self::Signed
fn cast_signed(self) -> Self::Signed
Cast an integer to the signed integer of the same size.