Crate upcast_arithmetic
source · [−]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
const
utilities for performing upcast arithmetic in const
.
Traits
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.
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.
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.
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.
Trait for the one value of a type.
Performs power (self ^ exp). Maps directly to the underlaying integer method.
Performs upcasting to type with higher (and/or lower) limits.
Trait to implement upcast add. (Casting up when type bounds are hit during arithmetic)
Trait to implement upcast mul. (Casting up when type bounds are hit during arithmetic)
Trait to perform upcast pow. (Casting up when type bounds are hit during arithmetic)
Trait to implement upcast sub. (Casting up when type bounds are hit during arithmetic)