Skip to main content

Crate reflect_nat

Crate reflect_nat 

Source
Expand description

Ready-made type-level values: Peano naturals, booleans, and heterogeneous lists, all implementing Reflect.

Where reify-reflect-core defines the Reflect trait and the RuntimeValue vocabulary, this crate provides the most useful concrete instances of that trait, the ones you reach for first when prototyping type-level code.

All of these types are zero-sized: they exist only at compile time and disappear at runtime. The Reflect impls are what give you a runtime handle on the value they encode.

§What’s in here

§Peano naturals

  • Z is zero, S<N> is “successor of N”.
  • Add, Mul, and Lt perform type-level arithmetic.
  • N0 through N8 are convenience aliases for the small numbers.
  • The Nat trait gives you a runtime u64 for any of these types, and the Reflect impl produces a RuntimeValue::Nat.

§Type-level booleans

  • True and False.
  • Not, And, Or perform compile-time boolean logic at the trait level.
  • Both reflect to a plain bool.

§Heterogeneous lists

§Optional bridges (feature-gated)

  • frunk: interoperate with frunk’s HList.
  • typenum: bridge between Nat and typenum’s Unsigned.

§Examples

use reflect_nat::{Z, S, True, HNil, HCons};
use reify_reflect_core::{Reflect, RuntimeValue};

// Type-level natural: 3
type Three = S<S<S<Z>>>;
assert_eq!(Three::reflect(), RuntimeValue::Nat(3));

// Type-level boolean
assert_eq!(True::reflect(), true);

// Type-level HList: [3, 0]
type MyList = HCons<Three, HCons<Z, HNil>>;
assert_eq!(
    MyList::reflect(),
    vec![RuntimeValue::Nat(3), RuntimeValue::Nat(0)]
);

See also: reflect-derive for #[derive(Reflect)] on your own structs and enums (which can include the types from this crate as fields), and const-reify for going the other direction (runtime u64 to const generic).

Structs§

False
Type-level false.
HCons
A cons cell for heterogeneous lists. HCons<H, T> prepends head H to tail T.
HNil
The empty heterogeneous list.
S
Type-level successor. S<N> represents N + 1.
True
Type-level true.
Z
Type-level zero.

Traits§

Add
Type-level addition. Add<A, B> computes A + B.
And
Type-level boolean AND.
Bool
Marker trait for type-level booleans.
HList
Marker trait for type-level heterogeneous lists.
Lt
Type-level less-than comparison. Lt<A, B> is true when A < B.
Mul
Type-level multiplication. Mul<A, B> computes A * B.
Nat
Marker trait for types that represent type-level natural numbers.
Not
Type-level boolean NOT.
Or
Type-level boolean OR.

Type Aliases§

N0
Type alias for 0.
N1
Type alias for 1.
N2
Type alias for 2.
N3
Type alias for 3.
N4
Type alias for 4.
N5
Type alias for 5.
N6
Type alias for 6.
N7
Type alias for 7.
N8
Type alias for 8.