smallnum
Integer optimization: macros return the smallest integer type capable of fitting a static bounds.
Both signed (e.g. macro input is maximum) and unsigned (e.g. macro input is maximum or minimum) numbers supported.
Saves memory on embedded devices.
!#[no_std], #![forbid(unsafe_code)], zero-cost.
Example: Statically-sized Collection Index
When the size of a collection is known at compile-time, the variable used to index it can be size-optimized.
use ;
use size_of_val;
const MAX_CAPACITY: usize = 500;
let my_array: = ;
let idx: usize = 5;
let small_idx: small_unsigned! = 5;
assert_eq!; // Equivalent values
assert_eq!; // Equivalent collection indexing
assert!; // Memory savings (6 bytes on 64-bit)
assert_eq!;
assert_eq!;
Example: Tree Node Metadata
When the maximum capacity of a tree is known at compile-time, metadata stored in every node can be size-optimized.
use small_unsigned;
use size_of;
const MAX_CAPACITY: usize = 500;
// Regular node in a binary tree
// Node with size-optimized metadata
// Per-node memory savings (16 bytes on 64-bit)
assert!;
assert_eq!;
assert_eq!;