Crate nonmaxunsigned

Source
Expand description

§NonMax-Unsigned

Crates.io docs.rs Dependency status

This crate provides an implementation for non-max integer types with null-pointer-optimization.

This crate is usable in a no-std environment.

§Example

use nonmaxunsigned::NonMaxU8;

fn main() {
    let a = NonMaxU8::new(5).unwrap();
    let b = NonMaxU8::new(12).unwrap();

    assert_eq!(NonMaxU8::new(17).unwrap(), a + b);
    assert_eq!(None, NonMaxU8::new(u8::MAX));
}

§Similar Crates

There are a few similar crates already existing on crates.io:

Unlike those crates this implementation does not NonZero. Those crates store the value as value ^ max which is never 0 as long as value != max. However this means the binary/hex representation of the stored value is hard to parse.

This implementation instead relies on a large enum NonMaxU8Internal which has 254 variants. Such an enum also leads to null-pointer-optimization but does not require the xor trick to prevent 0 values. Instead it relies on core::mem::transmute_copy.

§License

Licensed under either of

at your option.

Structs§

NonMaxU8
NonMaxU8 is a nonmax version of u8
NonMaxU16
NonMaxU16 is a nonmax version of u16
NonMaxU32
NonMaxU32 is a nonmax version of u32
NonMaxU64
NonMaxU64 is a nonmax version of u64
PrimitiveIsMaxError
Error type returned by TryFrom from primitive integer into nonmax versions