Expand description
This crate provides convenient casts between primitive integers.
The primary entry point is the extension trait IntCast, which will
add casting methods to the primitive integer types. The casts provided are:
x.cast(), only implemented when the cast is infallible,x.checked_cast(), returningNoneif the cast fails,x.strict_cast(), panicking if the cast fails,x.unchecked_cast(), undefined behavior if the cast fails,x.wrapping_cast(), never fails, wraps around, andx.saturating_cast(), never fails, saturates at the boundary.
Unlike x.into() and x.try_into() these casts can only
convert between primitive integer types, and they support directly writing the target type using
a turbofish, e.g. x.strict_cast::<usize>().
All cast traits are sealed meaning you may not implement them for custom types. This is intended, so that the behavior is predictable.
§Features
If you enable the nonzero feature all traits are extended with implementations for
core::num::NonZero types. Note that this requires a nightly compiler and uses the
very much unstable core::num::ZeroablePrimitive.
Traits§
- Bounded
Cast From Int - Conversion between integers, wrapping around or saturating at the target type’s boundaries.
- Cast
From Int - Infallible conversion between integers.
- Checked
Cast From Int - Fallible conversion between integers.
- IntCast
- Extension trait for casting integers.