Expand description
Saturating scalar wrappers for signed and unsigned primitive integers.
The crate exposes signed wrappers (Si8, Si16, Si32, Si64,
Si128, Sisize) and unsigned wrappers (Su8, Su16, Su32,
Su64, Su128, Susize). Arithmetic operators saturate at the
left-hand side type’s numeric bounds.
use satint::{Si8, Su8, sisize, si8, su8, susize};
assert_eq!(su8(250) + su8(10), Su8::MAX);
assert_eq!(su8(0) - 1, Su8::ZERO);
assert_eq!(si8(100) * 2, Si8::MAX);
assert_eq!(-Si8::MIN, Si8::MAX);
assert_eq!(sisize(-10) + susize(5), sisize(-5));Checked division and remainder are available as inherent checked_*
methods for same-type operands, and as TryDiv / TryRem traits when
the right-hand side is another same-sign wrapper width or a primitive.
use satint::{DivError, TryDiv, TryRem, si8, si16};
assert_eq!(si8(20).checked_div(si8(3)), Some(si8(6)));
assert_eq!(si8(20).try_div(si16(3)), Ok(si8(6)));
assert_eq!(si8(20).try_rem(3_i8), Ok(si8(2)));
assert_eq!(si8(20).try_div(0_i8), Err(DivError::DivisionByZero));Use From / Into for lossless conversions and SaturatingFrom /
SaturatingInto when the source may be out of range. Saturating
conversions are implemented between all primitive integer types, between
wrappers, and between wrappers and primitive integers.
use satint::{SaturatingFrom, SaturatingInto, Si8, Si32, Su8, si16, su8};
let widened: Si32 = Si8::new(-5).into();
let clamped: Su8 = si16(-1).saturating_into();
let from_float = Si8::saturating_from(200.0_f32);
let primitive_from_wrapper = u8::saturating_from(si16(300));
let primitive_from_primitive: i8 = i16::MAX.saturating_into();
let primitive_from_float = u8::saturating_from(-1.0_f64);
assert_eq!(widened, Si32::new(-5));
assert_eq!(clamped, Su8::ZERO);
assert_eq!(from_float, Si8::MAX);
assert_eq!(primitive_from_wrapper, u8::MAX);
assert_eq!(primitive_from_primitive, i8::MAX);
assert_eq!(primitive_from_float, 0);
assert_eq!(su8(42).to_signed(), Si8::new(42));Structs§
- Si8
- A saturating wrapper around
i8. - Si16
- A saturating wrapper around
i16. - Si32
- A saturating wrapper around
i32. - Si64
- A saturating wrapper around
i64. - Si128
- A saturating wrapper around
i128. - Sisize
- A saturating wrapper around
isize. - Su8
- A saturating wrapper around
u8. - Su16
- A saturating wrapper around
u16. - Su32
- A saturating wrapper around
u32. - Su64
- A saturating wrapper around
u64. - Su128
- A saturating wrapper around
u128. - Susize
- A saturating wrapper around
usize.
Enums§
- DivError
- Error returned by fallible division and remainder operations.
Traits§
- Saturating
From - Lossy conversion that clamps the value to the destination range.
- Saturating
Into - Reciprocal of
SaturatingFrom. Provided automatically via a blanket impl. - TryDiv
- Fallible division that distinguishes division by zero from overflow.
- TryDiv
Assign - Fallible division assignment.
- TryRem
- Fallible remainder that distinguishes division by zero from overflow.
- TryRem
Assign - Fallible remainder assignment.
Functions§
- si8
- Creates a
Si8from an inneri8value. - si16
- Creates a
Si16from an inneri16value. - si32
- Creates a
Si32from an inneri32value. - si64
- Creates a
Si64from an inneri64value. - si128
- Creates a
Si128from an inneri128value. - sisize
- Creates a
Sisizefrom an innerisizevalue. - su8
- Creates a
Su8from an inneru8value. - su16
- Creates a
Su16from an inneru16value. - su32
- Creates a
Su32from an inneru32value. - su64
- Creates a
Su64from an inneru64value. - su128
- Creates a
Su128from an inneru128value. - susize
- Creates a
Susizefrom an innerusizevalue.