Module dactyl::traits [−][src]
Dactyl: Saturated Unsigned Integer Conversion
The SaturatingFrom
trait allows large primitives to be downcast into smaller
types with values capped at the smaller type’s ::MAX
value, avoiding any
possible overflow or wrapping issues. It’s a clamp, basically, except all uints
share the same bottom.
It is implemented for u8
, u16
, u32
, and u64
for all types larger than
said type, up to u128
.
The usize
type, being variable, works a little differently. It implements
SaturatingFrom
on u32
, u64
, and u128
regardless of the machine’s bit
size, but its ceiling will vary based on the machine’s bit size (it could be as
low as u16::MAX
or as high as u64::MAX
).
Examples
pub use dactyl::traits::SaturatingFrom; assert_eq!(u8::saturating_from(1026_u16), 255_u8); assert_eq!(u8::saturating_from(99_u16), 99_u8);
Traits
SaturatingFrom | Saturating From. |