docs

You are reading the docs for v0.1.2. Nabi 0.1.2 is no longer supported. Latest release: v0.1.3 →

#Reflection

On this page

Nabi programs can look at their own types while they run: the fields of a struct with their types, an enum's variants, and attributes you attach for tools. This is what an editor's property panel, a save-game system or a generic serializer is built on. This chapter shows how to ask for a type's description, read and change fields through it, attach your own attributes, and save and load a whole scene with no hand-written code per type.

Every type already knows its name and size (T::type_name(), T::size()). The rest is opt-in: put #[derive(Inspect)] on the types you want to look into. A program that never asks about a type pays nothing for the derive.

#A type's description

T::type_info() returns a TypeInfo. It is read straight from data the compiler wrote into the program, so reading it allocates nothing. A value you want to read or change goes in an any.

  • fields() lists every field in order, private ones too: the derive is the type author's consent. field(name) finds one.
  • Field::get(owner) reads a field, get_as::<T>(owner) reads it as a T, and set(ref owner, value) writes one. A value of the wrong type is refused (false), but an integer that fits converts.
  • core::reflect::info_of(value) tells which type an any holds.
  • An enum's fields belong to its variants: variants(), variant_of(value), and Variant::fields().

#Your own attributes

A struct marked #[runtime_attribute(name)] becomes an attribute you can write on types, fields and variants. Its arguments are checked like a struct literal, so a mistake is a compile error that shows what to write. attr::<A>() reads the value back.

nabi
/// Shown as a slider in the editor.
#[runtime_attribute(range)]
pub struct Range { pub min: f64; pub max: f64; }

#[derive(Inspect)]
struct Enemy {
    #[range(0, 100)] hp: i32 = 100;
    #[range(min = 0.5, max = 4.0)] speed: f64 = 1.0;
    name: str = "orc";
}

fn main() {
    for f in Enemy::type_info().fields() {
        match f.attr::<Range>() {
            r: Range => println(f"{f.name()}: slider {r.min}..{r.max}"),
            none => println(f"{f.name()}: text box"),
        }
    }
}
hp: slider 0..100
speed: slider 0.5..4
name: text box

An attribute's fields are numbers, bool, char or str. #[inspect(skip)] hides a field from reflection, and #[renamed_from("old")] records a field's earlier name for code that reads old data.

#Saving and loading a scene

std::nabion::to_value_any turns any described value into a NABION Value (chapter 15), and from_value_any builds one back from a Value and a TypeInfo. Nothing in the scene types is written by hand except the derive. A field the text leaves out takes its default, as hp does for the rat.

? on a T | none works only in a function that may return none itself. In a function that throws, write x ?? throw err to turn a missing value into an error, as load does.

#When to use it

Reach for reflection when code must handle types it does not know, such as an editor, a debugger, a save format or a test fixture loader. For one known type, a #[derive(Nabion)] or a plain function is shorter and faster. The full API is in the standard library specification (2.12) and the rules in the language reference (6.10).

Found a mistake? Edit this page on GitHub. Last updated