Module fraction::dynaint

source ·
Expand description

Dynamic unsigned integer type selection

Implements a wrapper around two unsigned integer types that picks the size dynamically (at runtime) depending on the value.

Most useful in combination with heap allocated integers such as BigUint. If we expect small numbers to operate with, and we wish to work without allocations by default. However, we want to handle huge numbers gracefully, without stack overflows.

§Examples

use fraction::{dynaint::DynaInt, One};
type DI = DynaInt<u8, u16>;

let one: DI = DynaInt::one();
let mut num: DI = DynaInt::from(255u8);
assert_eq!(num.unpack(), Ok(255u8));

num += one;
assert_eq!(num.unpack(), Err(256u16));

num -= one;  // here it automatically detects the number fits into u8 again
assert_eq!(num.unpack(), Ok(255u8));

Enums§