const-reify 0.1.1

Safe runtime-to-const-generic dispatch via match tables
Documentation
  • Coverage
  • 100%
    20 out of 20 items documented14 out of 18 items with examples
  • Size
  • Source code size: 32.9 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.69 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2m Average build duration of successful builds.
  • all releases: 1m 22s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • joshburgess/reify-reflect
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • joshburgess

Use a runtime u64 as a const N: u64, safely.

Const generics in Rust have to be known at compile time. That's frustrating when the value you actually want to parameterize on (a modulus, a buffer size, a feature flag) only becomes known at runtime. The orthodox workarounds are to drop the const generic (and lose the type safety), or to write a giant match by hand.

This crate is the giant match, generated for you, with three progressively more powerful APIs on top.

Everything is #![deny(unsafe_code)]. There is no vtable fabrication, no transmute, no UB. The dispatch is a flat 256-arm match that the compiler optimizes well. The tradeoff: the runtime value must lie in 0..=255 per dispatch (and the trait this crate ships with is just one example; you can build your own with a wider range). For the full safety analysis and why we chose this over the original "fabricate a vtable" approach, see DESIGN.md.

Three APIs, in order of power

1. [reify_const] / [reify!]

Smallest surface area. You get a &dyn HasModulus whose modulus() returns your runtime value. The const generic is "real" inside the dispatch (each arm calls Modular::<N>::new()), but you can only see it through the [HasModulus] trait. Useful for testing the wiring.

use const_reify::{reify_const, HasModulus};

let result = reify_const(17, |m| m.modulus());
assert_eq!(result, 17);

2. [reify_nat_fn] / [reify_nat2_fn]

When you only need the runtime value as a plain u64 inside the callback (no const generic gymnastics needed). The closure form is the easiest entry point and is enough for most ad-hoc uses.

use const_reify::reify_nat_fn;

let squared = reify_nat_fn(12, |n| n * n);
assert_eq!(squared, 144);

3. [NatCallback] / [reify_nat]

The full power form. Implement [NatCallback] on a type, and inside call::<const N: u64>() the value N is a genuine const generic that you can use in const N: u64 positions.

use const_reify::nat_reify::{NatCallback, reify_nat};

struct Square;
impl NatCallback<u64> for Square {
    fn call<const N: u64>(&self) -> u64 { N * N }
}

assert_eq!(reify_nat(7, &Square), 49);

For traits with multiple const-generic methods, the #[reifiable] proc macro generates the NatCallback plumbing automatically. See Guide 4.

See also

  • docs/phase5-const-reify.md for the design rationale, including why the range is 256 and why this is fully safe despite the "vtable fabrication" reputation of the underlying technique.
  • The narrative blog post for a worked example using modular arithmetic.