Utility library for dealing with arithmetic on type limits by upcasting into types with higher limits.
let a = u8::MAX;
let b = 2u8;
let modulo = u8::MAX;
let res = (a + b) % modulo;
assert_eq!(res, 2);
use upcast_arithmetic::*;
let a = u8::MAX;
let b = 2u8;
let modulo = u8::MAX;
let res = a.upcast_add_mod(b, modulo);
assert_eq!(res, 2);
The performance overhead is very small. In benchmarks it seems like there is only a neglegible performance penelty, compared to assuming no overflow occurs.
The crate is fully #![no_std]
compatible.
There is no unsafe code and the flag #![deny(unsafe_code)]
is set.
License: MIT