CheckedNum
Overflow-checked numbers for safety without sacrificing ergonomics.
Usage
With checked_num
use CheckedU16;
assert_eq!
Without checked_num
assert!;
Features
-
#![no_std]enabled per default. -
Supports
NonZero<_>types for zero memory overhead. -
Only depends on
num-traits. -
Supports checked versions of
Add,Sub,Mul,Div,Rem,Shl,ShrandNeg -
Wraps
BitAnd,BitOr,BitXorandInvfor convenience.
Contributing
Areas for improvement:
-
Implement the
?operator. -
Implement checked casts.
-
Introduce a macro that ensures all arithmetic operations are checked, preventing unintended unchecked operations due to precedence issues.
-
Implement
num_traits::CheckedEuclidandnum_traits::MulAdd. -
Add
_Assignvariants for supported operations. -
Expand documentation.
-
Add more tests.
Limitations
Due to the orphan rule, CheckedNum types must appear on the left-hand side of mixed-type operations:
use CheckedU16;
let a = new;
let b = 210;
assert_eq! // correct
use CheckedU16;
let a = new;
let b = 210;
assert_eq! // fails to compile