Expand description
Utility library for dealing with arithmetic on type limits by upcasting into types with higher limits.
§Examples
§Without upcast_arithmetic
(panics)
ⓘ
let a = u8::MAX;
let b = 2u8;
let modulo = u8::MAX;
let res = (a + b) % modulo;
assert_eq!(res, 2);
§With upcast_arithmetic
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);
§Performance
The performance overhead is very small. In benchmarks it seems like there is only a neglegible performance penelty, compared to assuming no overflow occurs.
§no_std
The crate is fully #![no_std]
compatible.
§Unsafe
There is no unsafe code and the flag #![deny(unsafe_code)]
is set.
Modules§
- constant
const
- utilities for performing upcast arithmetic in
const
.
Traits§
- Checked
Add - Performs checked addition. Maps directly to the integers checked_add method. (i.e.
u8::checked_add
) Consult the docs of the primitive methods to learn more. - Checked
Mul - Performs checked multiplication. Maps directly to the integers checked_mul method. (i.e.
u8::checked_mul
) Consult the docs of the primitive methods to learn more. - Checked
Pow - Performs checked power. Maps directly to the integers checked_pow method. (i.e.
u8::checked_pow
) Consult the docs of the primitive methods to learn more. - Checked
Sub - Performs checked substraction. Maps directly to the integers checked_sub method. (i.e.
u8::checked_sub
) Consult the docs of the primitive methods to learn more. - One
- Trait for the one value of a type.
- Pow
- Performs power (self ^ exp). Maps directly to the underlaying integer method.
- Upcast
- Performs upcasting to type with higher (and/or lower) limits.
- Upcast
Add - Trait to implement upcast add. (Casting up when type bounds are hit during arithmetic)
- Upcast
Mul - Trait to implement upcast mul. (Casting up when type bounds are hit during arithmetic)
- Upcast
Pow - Trait to perform upcast pow. (Casting up when type bounds are hit during arithmetic)
- Upcast
Sub - Trait to implement upcast sub. (Casting up when type bounds are hit during arithmetic)