Skip to main content

Crate reflect_derive

Crate reflect_derive 

Source
Expand description

#[derive(Reflect)] for structs and enums.

This crate provides a single proc macro that generates a reify_reflect_core::Reflect implementation describing the structural shape of a type as a RuntimeValue tree.

It pairs naturally with reflect-nat: use type-level naturals, booleans, or HLists as fields, and the derive will recursively reflect them into a single value-level description of the type.

Note that this reflects the type schema, not a particular runtime instance. The generated reflect() is a static method (matching the reify_reflect_core::Reflect trait), so it walks every variant and every variant’s field types.

§Encoding

The generated reflect() returns:

  • Named struct => RuntimeValue::List of (field_name_bytes, field_value) pairs.
  • Tuple struct => RuntimeValue::List of positional field values.
  • Unit struct (struct X;) => RuntimeValue::Unit.
  • Empty named struct (struct X {}) => RuntimeValue::List(vec![]).
  • Enum => RuntimeValue::List of variant entries, each (variant_name_bytes, variant_payload), where the payload mirrors the per-variant shape (unit / tuple / named).

Field names are encoded as a RuntimeValue::List of byte values to keep RuntimeValue minimal, rather than introducing a new string variant. See docs/phase1-foundations.md for the rationale.

§Field attributes

  • #[reflect(skip)] omits a field from the generated output. Fields whose types do not implement Reflect<Value = RuntimeValue> must be skipped (or the type changed) for the derive to compile.

§Examples

use reflect_derive::Reflect;
use reify_reflect_core::{Reflect, RuntimeValue};
use reflect_nat::{S, Z};

#[derive(Reflect)]
struct Point {
    #[reflect(skip)]
    label: String,
    x: S<S<Z>>,        // type-level 2
    y: S<S<S<Z>>>,     // type-level 3
}

// Reflects to a List of (b"x", Nat(2)), (b"y", Nat(3)).
let _shape: RuntimeValue = <Point as Reflect>::reflect();

Derive Macros§

Reflect
Derives the Reflect trait for a struct or enum.