Expand description
Integer-to-integer conversions: sign reinterpretation, absolute
difference, value-preserving widening, lossy truncation, and the generic
cast family, mirroring cast_signed / cast_unsigned / unsigned_abs /
abs_diff / widen / truncate /
{checked,wrapping,saturating,strict}_cast / clamp_magnitude on the
primitive integer types.
The checked/saturating/strict sign-cast and truncate variants found
in std are NOT mirrored as methods here: they are exactly
the generic cast traits (CheckedCast, SaturatingCast,
StrictCast) applied to the counterpart type, so the sign-cast and
truncate traits stay one-method (the Tier-A bit-pattern operations) and
the generic casts are the single home for value-checked conversion.
Stability in std (as of nightly 2026): unsigned_abs (1.51) and
abs_diff (1.60) are delegated; cast_signed/cast_unsigned (1.87),
widen/truncate (integer_widen_truncate), the generic *_cast
family (integer_casts) and clamp_magnitude are newer than the
crate’s MSRV or still nightly-only, and are hand-rolled with the same
semantics.
CT tiers: CastSigned/CastUnsigned, Truncate, Widen,
WrappingCast, AbsDiff and UnsignedAbs are Tier A
(bit-pattern / borrow-free operations); CheckedCast,
SaturatingCast and ClampMagnitude are Tier B; StrictCast is
Tier C (data-dependent panic).
Traits§
- AbsDiff
- Computes the absolute difference of two values.
- Cast
Signed - Reinterprets an unsigned integer’s bits as its signed counterpart.
- Cast
Unsigned - Reinterprets a signed integer’s bits as its unsigned counterpart.
- Checked
Cast - Fallible value-preserving conversion between any two integer types
(the trait counterpart of std’s generic
checked_cast). - Clamp
Magnitude - Clamps the magnitude (absolute value) of a signed integer.
- Saturating
Cast - Saturating conversion between any two integer types
(std’s generic
saturating_cast). - Strict
Cast - Value-preserving conversion between any two integer types that panics on
overflow, even in release builds (std’s generic
strict_cast). - Truncate
- Truncates to an integer of the same signedness and the same size or smaller, discarding high-order bits.
- Unsigned
Abs - Computes the absolute value as the unsigned counterpart type, which
cannot overflow (unlike
abs, which overflows onMIN). - Widen
- Widens to an integer of the same signedness and the same size or larger, always preserving the value.
- Wrapping
Cast - Wrapping conversion between any two integer types
(std’s generic
wrapping_cast— the semantics ofas).