num_conv

Trait Truncate

Source
pub trait Truncate: Integer {
    // Required method
    fn truncate<T>(self) -> T
       where Self: TruncateTarget<T>;
}
Expand description

Truncate to an integer of the same size or smaller, preserving the least significant bits.

assert_eq!(u16::MAX.truncate::<u8>(), u8::MAX);
assert_eq!(u32::MAX.truncate::<u16>(), u16::MAX);
assert_eq!(u64::MAX.truncate::<u32>(), u32::MAX);
assert_eq!(u128::MAX.truncate::<u64>(), u64::MAX);
assert_eq!((-1_i16).truncate::<i8>(), -1_i8);
assert_eq!((-1_i32).truncate::<i16>(), -1_i16);
assert_eq!((-1_i64).truncate::<i32>(), -1_i32);
assert_eq!((-1_i128).truncate::<i64>(), -1_i64);

Required Methods§

Source

fn truncate<T>(self) -> T
where Self: TruncateTarget<T>,

Truncate an integer to an integer of the same size or smaller, preserving the least significant bits.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Integer> Truncate for T