This crate provides type-level natural numbers, similar to typenum.
A type-level number is a type that represents a number. The Nat trait takes the role of the
“type-level number type”, i.e. one accepts a type-level number using a generic parameter with
bound Nat.
The use cases are the same as those of generic consts.
gnat differs from typenum in that its Nat trait is not a marker trait, but defines
enough structure (through hidden generic associated types) to be able to define and use
generic operations on it, without any extra bounds.
As such, this crate is more expressive than typenum and generic_const_exprs.
For details about defining custom operations, see the expr module documentation.
Some 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 combersome and leaks into the bounds of every other calling function.
Since this crate does not require such bounds, the naive implementation just works:
assert_eq!; // 10 5 2 1 0