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§