Truncate

Trait Truncate 

Source
pub trait Truncate: Sealed + Sized {
    // Required method
    fn truncate<T: TruncateFrom<Self>>(self) -> T;
}
Expand description

Trait to truncate an integer from a larger size.

This is better than as casting because:

  • It is explicitly a truncating operation, and will only truncate
  • It only supports similar signs, i/e u16 to i8 will not compile
  • It is method chainable
  • You can use turbofishy or type inference :D

Error messages should also be clear in the event of an invalid operation, so you will not be left wondering what went wrong, this is mostly thanks to rusts great error messages though

Required Methods§

Source

fn truncate<T: TruncateFrom<Self>>(self) -> T

Truncates an integer to a smaller integer

§Examples
let u8_val = 0u16.truncate::<u8>();

But this wont compile:

let val: u32 = 0u16.truncate();

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.

Implementations on Foreign Types§

Source§

impl Truncate for i8

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Source§

impl Truncate for i16

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Source§

impl Truncate for i32

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Source§

impl Truncate for i64

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Source§

impl Truncate for i128

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Source§

impl Truncate for u8

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Source§

impl Truncate for u16

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Source§

impl Truncate for u32

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Source§

impl Truncate for u64

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Source§

impl Truncate for u128

Source§

fn truncate<T: TruncateFrom<Self>>(self) -> T

Implementors§