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§
Required Associated Types§
Required Methods§
Sourcefn state_ref<'a>(
doc: &'a DocCell,
base: Path,
sink: PatchSink<'a>,
) -> Self::Ref<'a>
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 frombase- The base path for this statesink- The operation collector
Sourcefn from_value(value: &Value) -> TireaResult<Self>
fn from_value(value: &Value) -> TireaResult<Self>
Deserialize this type from a JSON value.
Sourcefn to_value(&self) -> TireaResult<Value>
fn to_value(&self) -> TireaResult<Value>
Serialize this type to a JSON value.
Provided Methods§
Sourcefn register_lattice(_registry: &mut LatticeRegistry)
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).
Sourcefn lattice_keys() -> &'static [&'static str]
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).
Sourcefn diff_ops(old: &Self, new: &Self, base_path: &Path) -> TireaResult<Vec<Op>>
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.
Sourcefn to_patch(&self) -> TireaResult<Patch>
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.