reflect-core
Core trait definitions for type-level reification and reflection.
This crate provides the foundational [Reflect] trait, the [reify]
function, and the [RuntimeValue] enum.
The Reification/Reflection Pattern
This implements the pattern from Kiselyov & Shan's "Functional Pearl:
Implicit Configurations" (popularized by Kmett's Haskell reflection
library), adapted to Rust with branded lifetimes for scoping safety.
-
[
Reflect]: Type-level value → runtime value. A type that encodes a value at the type level can produce it at runtime. -
[
reify]: Runtime value → scoped type-level context. Takes a runtime value and passes it to a continuation as a branded [Reified] token. The branded lifetime prevents the token from escaping the callback.
Haskell comparison
In Haskell:
In Rust, the forall s is modeled by an invariant lifetime 'brand
on [Reified]. The higher-rank bound for<'brand> on the callback
ensures the token cannot escape, just as Haskell's rank-2 type
prevents s from escaping.
Unlike Haskell's reflection library (which uses unsafeCoerce to
fabricate typeclass dictionaries from GHC internals), this implementation
is safe all the way down: no unsafe code, no compiler-internal
assumptions, scoping enforced mechanically by the borrow checker.
Examples
use reify;
// Lift a runtime value into a scoped type-level context
let result = reify;
assert_eq!;