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§
Sourcefn cast<T: CastFromInt<Self>>(self) -> T
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.
Sourcefn saturating_cast<T: BoundedCastFromInt<Self>>(self) -> T
fn saturating_cast<T: BoundedCastFromInt<Self>>(self) -> T
Converts self to the target integer type, saturating at the numeric
bounds instead of overflowing.
Sourcefn wrapping_cast<T: BoundedCastFromInt<Self>>(self) -> T
fn wrapping_cast<T: BoundedCastFromInt<Self>>(self) -> T
Converts self to the target integer type, wrapping around at the
boundary of the target type.
Sourcefn checked_cast<T: CheckedCastFromInt<Self>>(self) -> Option<T>
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.
Sourcefn strict_cast<T: CheckedCastFromInt<Self>>(self) -> T
fn strict_cast<T: CheckedCastFromInt<Self>>(self) -> T
Equivalent to self.checked_cast::<T>().unwrap().
Sourceunsafe fn unchecked_cast<T: CheckedCastFromInt<Self>>(self) -> T
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.