State

Trait State 

Source
pub trait State: Sized {
    type Ref<'a>;

    const PATH: &'static str = "";

    // Required methods
    fn state_ref<'a>(
        doc: &'a DocCell,
        base: Path,
        sink: PatchSink<'a>,
    ) -> Self::Ref<'a>;
    fn from_value(value: &Value) -> TireaResult<Self>;
    fn to_value(&self) -> TireaResult<Value>;

    // Provided methods
    fn register_lattice(_registry: &mut LatticeRegistry) { ... }
    fn lattice_keys() -> &'static [&'static str] { ... }
    fn diff_ops(
        old: &Self,
        new: &Self,
        base_path: &Path,
    ) -> TireaResult<Vec<Op>> { ... }
    fn to_patch(&self) -> TireaResult<Patch> { ... }
}
Expand description

Trait for types that can create typed state references.

This trait is typically derived using #[derive(State)]. It provides the interface for creating StateRef types that allow typed read/write access to JSON documents.

§Example

use tirea_state::State;
use tirea_state_derive::State;

#[derive(State)]
struct User {
    pub name: String,
    pub age: i64,
}

// In a StateContext:
let user = ctx.state::<User>("users.alice");
let name = user.name()?;
user.set_name("Alice");
user.set_age(30);

Provided Associated Constants§

Source

const PATH: &'static str = ""

Canonical JSON path for this state type.

When set via #[tirea(path = "...")], enables state_of::<T>() access without an explicit path argument. Empty string means no bound path.

Required Associated Types§

Source

type Ref<'a>

The reference type that provides typed access.

Required Methods§

Source

fn state_ref<'a>( doc: &'a DocCell, base: Path, sink: PatchSink<'a>, ) -> Self::Ref<'a>

Create a state reference at the specified path.

§Arguments
  • doc - The JSON document to read from
  • base - The base path for this state
  • sink - The operation collector
Source

fn from_value(value: &Value) -> TireaResult<Self>

Deserialize this type from a JSON value.

Source

fn to_value(&self) -> TireaResult<Value>

Serialize this type to a JSON value.

Provided Methods§

Source

fn register_lattice(_registry: &mut LatticeRegistry)

Register lattice fields into the given registry.

Auto-generated by #[derive(State)] for structs with #[tirea(lattice)] fields. The default implementation is a no-op (no lattice fields).

Source

fn lattice_keys() -> &'static [&'static str]

Return the JSON keys of fields annotated with #[tirea(lattice)].

Used by the reducer pipeline to emit Op::LatticeMerge (instead of Op::set) for CRDT fields, enabling proper conflict suppression. The default implementation returns an empty slice (no lattice fields).

Source

fn diff_ops(old: &Self, new: &Self, base_path: &Path) -> TireaResult<Vec<Op>>

Compare two instances and emit minimal ops for changed fields.

The derive macro generates an optimized version that does typed per-field comparison and only serializes changed fields. Lattice fields emit Op::LatticeMerge; regular fields emit Op::Set.

The default implementation serializes both values and diffs at JSON level. When lattice_keys() is non-empty, changed lattice fields emit Op::LatticeMerge; all others emit Op::Set.

Source

fn to_patch(&self) -> TireaResult<Patch>

Create a patch that sets this value at the root.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§