Crate upcast_arithmetic

Source
Expand description

Crates.io docs.rs GitHub license Rust

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§

constantconst
utilities for performing upcast arithmetic in const.

Traits§

CheckedAdd
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.
CheckedMul
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.
CheckedPow
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.
CheckedSub
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.
UpcastAdd
Trait to implement upcast add. (Casting up when type bounds are hit during arithmetic)
UpcastMul
Trait to implement upcast mul. (Casting up when type bounds are hit during arithmetic)
UpcastPow
Trait to perform upcast pow. (Casting up when type bounds are hit during arithmetic)
UpcastSub
Trait to implement upcast sub. (Casting up when type bounds are hit during arithmetic)