name-index 0.2.1

Library for accessing struct fields by name at runtime
Documentation
use name_index::{NameIndex, NameIndexCopy};

#[derive(Default, NameIndex)]
struct CoolStruct {
    #[index]
    apple: bool,
    b: bool,
    #[alias(c,cl)]
    cool: bool,
    #[allow(dead_code)]
    other: String,
}

pub fn main() {
    let mut cool_struct = CoolStruct::default();
    cool_struct.b = true;

    let b = cool_struct.get("b").expect("'b' is a field of CoolStruct");
    cool_struct
        .set("cl", b)
        .expect("'cl' is an alias of the 'cool' field of CoolStruct");

    cool_struct
        .fields_mut()
        .into_iter()
        .for_each(|(_, r)| *r = !*r);

    for (name, value) in cool_struct.fields() {
        println!("{}: {}", name, value);
    }

    println!("'cool' has aliases: {:?}", cool_struct.field_aliases("cool"));

    let resolve_cl = cool_struct
        .resolve_alias("cl")
        .expect("'cl' is an alias of the 'cool' field of CoolStruct");
    println!("'cl' is actually aliased to '{}'", resolve_cl);
}