This crate provides type-level natural numbers, similar to typenum.
A type-level number is a type that represents a number. The Nat trait functions as the
“meta-type” of type-level numbers, i.e. to accept a type-level number, use a generic
parameter N: Nat.
The use cases are the same as those of generic consts.
Why this crate?
This crate differs from typenum in that Nat is not just a marker trait.
To perform arbitrary operations on a generic N: Nat, no extra bounds are
required.
As a result, this crate is more expressive than typenum or the
generic_const_exprs feature.
Motivating examples
Concatenating arrays at compile time
use ;
const >
To even spell out the type of the resulting array, generic_const_exprs and typenum+generic-array
require bounds to specify that the numbers can be added.
These bounds leak into all generic code that wants to use the function.
const
where
:, // Required well-formedness bound
use ;
const >
where
M: Add, // Need explicit bound for `+`
Const Recursion
assert_eq!; // 10 5 2 1 0
Naively writing a function that recurses over a const parameter is impossible in
generic_const_exprs and typenum, since the recursive argument needs the same
bounds as the parameter, i.e. the bounds of the function leak into itself.
}
use ;
Expressing this requires a recursive helper trait like trait RecDiv2: Unsigned { type Output: RecDiv2; },
which is cumbersome and leaks implementation details of the function into all generic callers
(since they have to copy this bound).
More about this crate is explained in the docs.