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?
gnat differs from typenum in that Nat is not just a marker trait.
It is sufficient for generic operations, without any extra bounds.
This includes custom operations, see the expr module docs.
Motivating examples
Concatenating arrays at compile time
Using generic_const_exprs or typenum/generic-array:
const
where
:, // Required well-formedness bound
use ;
const >
where // ArrayLength is not enough, we also need to add a bound for `+`
M: Add,
Using this crate:
use ;
const >
Const Recursion
Naively writing a function that recurses over the const parameter is impossible in
generic_const_exprs and typenum, since the recursive argument needs the same
bounds as the parameter:
}
use ;
While this can be expressed using a helper trait like trait RecDiv2: Unsigned { type Output: RecDiv2; },
it is cumbersome and leaks into the bounds of every other calling function.
Using this crate, the naive implementation without bounds just works:
assert_eq!; // 10 5 2 1 0