num_conv

Trait 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§

Source

type Signed

The signed integer type with the same size as Self.

Required Methods§

Source

fn cast_signed(self) -> Self::Signed

Cast an integer to the signed integer of the same size.

Implementations on Foreign Types§

Source§

impl CastSigned for i8

Source§

impl CastSigned for i16

Source§

impl CastSigned for i32

Source§

impl CastSigned for i64

Source§

impl CastSigned for i128

Source§

impl CastSigned for isize

Source§

impl CastSigned for u8

Source§

impl CastSigned for u16

Source§

impl CastSigned for u32

Source§

impl CastSigned for u64

Source§

impl CastSigned for u128

Source§

impl CastSigned for usize

Implementors§