Skip to main content

Crate const_reify_derive

Crate const_reify_derive 

Source
Expand description

#[reifiable]: turn a trait with const-generic methods into a runtime dispatch table.

const-reify gives you a primitive way to lift a runtime u64 into a const N: u64: implement NatCallback and call reify_nat. That works, but it gets verbose if a single trait has several const-generic methods: you end up writing one NatCallback impl per method.

#[reifiable(range = 0..=255)] on the trait declaration eliminates that boilerplate. The macro:

  • Generates a reify_<method_name> dispatch function for each const-generic method. The function takes a runtime u64, picks the matching monomorphization, and forwards.
  • Generates the NatCallback wrapper structs that reify_nat needs internally.
  • Leaves non-const-generic methods alone.

You then implement the trait normally, and call the generated reify_* dispatch functions from runtime code.

See Guide 4: the #[reifiable] macro for a full worked example, and docs/rfcs/0003-reifiable-proc-macro.md for the design.

§Example

use const_reify_derive::reifiable;

#[reifiable(range = 0..=255)]
trait ModArith {
    fn pow_mod<const N: u64>(&self, base: u64, exp: u64) -> u64;
    fn mul_mod<const N: u64>(&self, a: u64, b: u64) -> u64;
    fn name(&self) -> &str;  // not const-generic, left alone
}

struct FastMod;
impl ModArith for FastMod {
    fn pow_mod<const N: u64>(&self, base: u64, exp: u64) -> u64 { /* ... */ 0 }
    fn mul_mod<const N: u64>(&self, a: u64, b: u64) -> u64 { (a * b) % N }
    fn name(&self) -> &str { "fast" }
}

// Now `reify_pow_mod` and `reify_mul_mod` are generated dispatchers:
let modulus: u64 = 13;
let result = reify_pow_mod(modulus, &FastMod, 2, 12);

Attribute Macros§

reifiable
Generates const-generic dispatch functions for a trait’s methods.