Saturating integer scalar wrappers
satint provides small no_std, no-alloc wrappers around Rust primitive
integers that use saturating arithmetic for ordinary integer operations.
The crate exposes signed aliases (Si8, Si16, Si32, Si64, Si128) and
unsigned aliases (Su8, Su16, Su32, Su64, Su128), plus matching
constructor functions (si8, su32, and so on).
Why
Rust already has primitive methods like u8::saturating_add, but using them
consistently can be noisy when a value should saturate by default. satint
makes that behavior part of the type:
use ;
assert_eq!;
assert_eq!;
let mut health = MAX;
health += su8;
assert_eq!;
Supported Types
Signed:
use ;
use ;
Unsigned:
use ;
use ;
Each alias is a transparent wrapper over core::num::Saturating<T>.
Arithmetic
+, -, and * saturate for same-width values:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Primitive right-hand sides are also supported:
use ;
let mut value = su16;
value += 5;
value *= 3;
assert_eq!;
assert_eq!;
Division And Remainder
Division and remainder are intentionally checked methods rather than / and
% operator impls. They return None for division by zero, and for signed
overflow such as MIN / -1.
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Conversions
Lossless widening conversions use From / Into:
use ;
let signed: Si32 = si8.into;
let unsigned: Su32 = su8.into;
let unsigned_to_signed: Si32 = new.into;
assert_eq!;
assert_eq!;
assert_eq!;
Fallible narrowing and cross-sign conversions use TryFrom:
use ;
assert_eq!;
assert!;
assert_eq!;
assert!;
Clamping conversions use SaturatingFrom or SaturatingInto:
use ;
assert_eq!;
assert_eq!;
let value: Su8 = su16.saturating_into;
assert_eq!;
Widening Arithmetic
Same-sign wider-left-hand-side + and - are supported when the right-hand
side always fits in the left-hand side:
use ;
assert_eq!;
assert_eq!;
This is intentionally one-directional: the wider type must be on the left.
Constants And Iterators
Concrete aliases provide MIN, MAX, ZERO, and ONE.
Sum and Product are implemented for scalar values and references:
use ;
let values = ;
assert_eq!;
assert_eq!;
Optional serde Support
Enable the serde feature to serialize and deserialize scalar values as their inner integer representation:
[]
= { = "0.1", = ["serde"] }
The feature depends on serde without enabling serde's default features, so it remains compatible with no_std users.
no_std
satint is #![no_std] and does not use alloc.
Panics
No operation in this crate panics. Arithmetic operators saturate, division and
remainder are exposed only as checked_div / checked_rem returning Option,
and conversions are either lossless (From), fallible (TryFrom), or
clamping (SaturatingFrom / SaturatingInto). The crate is also
#![forbid(unsafe_code)].
Limitations
- This crate is for integer types only. Floats do not have integer-style saturating bounds.
- Only
+,-,*, their assignment forms, and signed unary-are operator overloads. - Division and remainder are available only through
checked_divandchecked_rem. - Cross-width arithmetic is limited to same-sign wider-left-hand-side
+and-. - Mixed signed/unsigned arithmetic is not implemented directly. Convert first
with
From,TryFrom, or the saturating conversion traits. - Saturation is not error reporting. If you need to detect overflow, use primitive checked arithmetic or fallible conversions where appropriate.
License
Licensed under the MIT or the Apache license.