Trackable

Derive Macro Trackable 

Source
#[derive(Trackable)]
{
    // Attributes available to this derive:
    #[x_bow]
    #[track]
}
Expand description

Macro to allows building paths to fields inside a struct/enum.

// Derive `Trackable` to allow parts of the struct to be tracked.
#[derive(Default, Trackable)]
struct MyStruct {
    field_1: i32,
    field_2: u64
}

// Create a centralized store with the data.
let store = Store::new(MyStruct::default());

// Build a path to the `i32` at `field_1` in the `MyStruct`.
let path = store.build_path().field_1();

§Shallow and Deep Tracking

By default, the Trackable derive macro makes tracking “shallow”. This means you can build path into each child field of a struct/enum, but if the type in a child field is itself a struct/enum, you won’t be able to continue your path into a grandchild field.

#[derive(Default, Trackable)]
struct MyStruct {
    field_1: ChildStruct,
    field_2: u64
}
#[derive(Default)]
struct ChildStruct {
    asdf: String
}

let store = Store::new(MyStruct::default());

store.build_path().field_1(); // OK
store.build_path().field_1().asdf(); // cannot do this!!

To allow building paths deep into descendants, enable deep tracking

#[derive(Default, Trackable)]
struct MyStruct {
    #[track(deep)] // 👈 enable deep tracking
    field_1: ChildStruct,
    field_2: u64
}
#[derive(Default, Trackable /* 👈 the subject of deep tracking must be Trackable */)]
struct ChildStruct {
    asdf: String
}

let store = Store::new(MyStruct::default());

store.build_path().field_1(); // OK
store.build_path().field_1().asdf(); // OK

Often, you will end up with #[track(deep)] on most (if not all) your fields. In this case, you can apply the attribute on the struct/enum itself. The attribute #[track(shallow)] can then be applied on individual fields to opt out of deep tracking.

#[derive(Trackable)]
#[track(deep)] // 👈 enable deep tracking
struct MyStruct {
    field_1: ChildStruct,
    #[track(shallow)] // 👈 don't track this field deeply
    field_2: u64
}

§Tracking Enums

As implied along this doc, the Trackable derive macro works on enums too.

#[derive(Trackable)]
enum MyEnum {
    Variant1 (String),
    Variant2 {
        field_a: i32,
        field_b: u64
    }
}
let store = Store::new(MyEnum::Variant1 ("Hello".into()));

let path = store.build_path().Variant1_0(); // <- the `String` in Variant1
let path = store.build_path().Variant2_field_a(); // <- the `i32` in Variant2