Skip to main content

IntCast

Trait IntCast 

Source
pub trait IntCast: Sealed {
    // Provided methods
    fn cast<T: CastFromInt<Self>>(self) -> T { ... }
    fn saturating_cast<T: BoundedCastFromInt<Self>>(self) -> T { ... }
    fn wrapping_cast<T: BoundedCastFromInt<Self>>(self) -> T { ... }
    fn checked_cast<T: CheckedCastFromInt<Self>>(self) -> Option<T> { ... }
    fn strict_cast<T: CheckedCastFromInt<Self>>(self) -> T { ... }
    unsafe fn unchecked_cast<T: CheckedCastFromInt<Self>>(self) -> T { ... }
}
Expand description

Extension trait for casting integers.

Provided Methods§

Source

fn cast<T: CastFromInt<Self>>(self) -> T

Converts self to the target integer type, without fail.

Note that this is not implemented for all type combinations, only if an infallible conversion exists.

Source

fn saturating_cast<T: BoundedCastFromInt<Self>>(self) -> T

Converts self to the target integer type, saturating at the numeric bounds instead of overflowing.

Source

fn wrapping_cast<T: BoundedCastFromInt<Self>>(self) -> T

Converts self to the target integer type, wrapping around at the boundary of the target type.

Source

fn checked_cast<T: CheckedCastFromInt<Self>>(self) -> Option<T>

Converts self to the target integer type, returning None if the value does not lie in the target type’s domain.

Source

fn strict_cast<T: CheckedCastFromInt<Self>>(self) -> T

Equivalent to self.checked_cast::<T>().unwrap().

Source

unsafe fn unchecked_cast<T: CheckedCastFromInt<Self>>(self) -> T

Equivalent to self.checked_cast::<T>().unwrap_unchecked().

§Safety

This results in undefined behavior when self will overflow when converted to the target type.

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 IntCast for i8

Source§

impl IntCast for i16

Source§

impl IntCast for i32

Source§

impl IntCast for i64

Source§

impl IntCast for i128

Source§

impl IntCast for isize

Source§

impl IntCast for u8

Source§

impl IntCast for u16

Source§

impl IntCast for u32

Source§

impl IntCast for u64

Source§

impl IntCast for u128

Source§

impl IntCast for usize

Implementors§