State

Derive Macro State 

Source
#[derive(State)]
{
    // Attributes available to this derive:
    #[tirea]
}
Expand description

Derive the State trait for a struct.

This macro generates:

  • A reference type {StructName}Ref<'a> with typed getter and setter methods
  • impl State for {StructName}

§Attributes

§Field Attributes

  • #[tirea(rename = "json_name")]: Use a different name in JSON
  • #[tirea(default = "expr")]: Default value expression if field is missing
  • #[tirea(skip)]: Exclude from state ref (field must implement Default)
  • #[tirea(nested)]: Treat as nested State. Required for struct fields that should have their own Ref type. Without this, the field is serialized as a whole value.
  • #[tirea(flatten)]: Flatten nested struct fields into parent

§Examples

use tirea_state::{State, StateContext};
use tirea_state_derive::State;

#[derive(State)]
struct Counter {
    value: i64,
    #[tirea(rename = "display_name")]
    label: String,
}

// Usage in a StateContext
let counter = ctx.state::<Counter>("counters.main");

// Read
let value = counter.value()?;
let label = counter.label()?;

// Write (automatically collected)
counter.set_value(100);
counter.set_label("Updated");
counter.increment_value(1);