Expand description
NonMax provides integer types that cannot be the maximum value of their underlying primitive type.
§Memory Optimization
The main benefit of NonMax<T> is that Option<NonMax<T>> has the same size as T.
This is achieved through Rust’s “niche optimization”, where the bit pattern of the
maximum value is used to represent None.
| Primitive | size_of::<T>() | size_of::<Option<T>>() | size_of::<Option<NonMax<T>>>() |
|---|---|---|---|
u32 | 4 | 8 | 4 |
i32 | 4 | 8 | 4 |
u8 | 1 | 2 | 1 |
§Examples
use arithmetic_nonmax::*;
use core::mem::size_of;
// The size of Option<NonMax*> is the same as the underlying primitive type.
assert_eq!(size_of::<Option<NonMaxU32>>(), 4);
assert_eq!(size_of::<Option<u32>>(), 8);
assert_eq!(size_of::<Option<NonMaxI32>>(), 4);
assert_eq!(size_of::<Option<i32>>(), 8);
assert_eq!(size_of::<Option<NonMaxU8>>(), 1);
assert_eq!(size_of::<Option<u8>>(), 2);Macros§
- non_max
- Creates a
NonMaxvalue at compile-time.
Structs§
- MaxValue
Error - Error type returned when a value is the maximum for its type.
- NonMax
- A wrapper type for an integer that cannot be its maximum value.
Type Aliases§
- NonMax
I8 - A signed 8-bit integer that cannot be
i8::MAX. - NonMax
I16 - A signed 16-bit integer that cannot be
i16::MAX. - NonMax
I32 - A signed 32-bit integer that cannot be
i32::MAX. - NonMax
I64 - A signed 64-bit integer that cannot be
i64::MAX. - NonMax
I128 - A signed 128-bit integer that cannot be
i128::MAX. - NonMax
Isize - A signed pointer-sized integer that cannot be
isize::MAX. - NonMax
U8 - An unsigned 8-bit integer that cannot be
u8::MAX. - NonMax
U16 - An unsigned 16-bit integer that cannot be
u16::MAX. - NonMax
U32 - An unsigned 32-bit integer that cannot be
u32::MAX. - NonMax
U64 - An unsigned 64-bit integer that cannot be
u64::MAX. - NonMax
U128 - An unsigned 128-bit integer that cannot be
u128::MAX. - NonMax
Usize - An unsigned pointer-sized integer that cannot be
usize::MAX.