Skip to main content

State

Struct State 

Source
pub struct State { /* private fields */ }
Expand description

A concurrent, type-safe state container that agents read from and write to.

By default, set() writes directly to the inner store. When delta tracking is enabled via with_delta_tracking(), writes go to a separate delta map that can be committed or rolled back.

Implementations§

Source§

impl State

Source

pub fn new() -> Self

Create a new empty state container.

Source

pub fn with_delta_tracking(&self) -> State

Create a new State with delta tracking enabled. Writes go to the delta map; reads check delta first, then inner.

Source

pub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>

Get a value by key, attempting to deserialize to the requested type. When delta tracking is enabled, checks delta first, then inner.

Source

pub fn with<F, R>(&self, key: &str, f: F) -> Option<R>
where F: FnOnce(&Value) -> R,

Borrow a value by key without cloning, applying f to the reference.

This is the zero-copy alternative to get_raw(). The closure receives a &Value directly from the DashMap ref-guard, avoiding the Value::clone() + serde_json::from_value() overhead of get().

Lookup order: delta (if tracking) → inner → derived fallback.

Source

pub fn get_raw(&self, key: &str) -> Option<Value>

Get a raw JSON value by key. When delta tracking is enabled, checks delta first, then inner. If the key is not found and doesn’t contain a prefix, also checks derived:{key} as a transparent fallback for computed variables.

Source

pub fn get_key<T: DeserializeOwned>(&self, key: &StateKey<T>) -> Option<T>

Get a typed value using a StateKey<T>.

Source

pub fn set_key<T: Serialize>(&self, key: &StateKey<T>, value: T)

Set a typed value using a StateKey<T>.

Source

pub fn with_key<T, F, R>(&self, key: &StateKey<T>, f: F) -> Option<R>
where F: FnOnce(&Value) -> R,

Zero-copy borrow using a StateKey<T>.

Source

pub fn set(&self, key: impl Into<String>, value: impl Serialize)

Set a value by key. When delta tracking is enabled, writes to delta instead of inner.

Source

pub fn set_committed(&self, key: impl Into<String>, value: impl Serialize)

Set a value directly in the committed store, bypassing delta tracking.

Source

pub fn modify<T, F>(&self, key: &str, default: T, f: F) -> T
where T: Serialize + DeserializeOwned, F: FnOnce(T) -> T,

Atomically read-modify-write a value.

If the key doesn’t exist, default is used as the initial value. The function f receives the current value and returns the new value. Returns the new value after modification.

Source

pub fn contains(&self, key: &str) -> bool

Check if a key exists (in delta or inner).

Source

pub fn remove(&self, key: &str) -> Option<Value>

Remove a key.

Source

pub fn keys(&self) -> Vec<String>

Get all keys (from both inner and delta when tracking).

Source

pub fn pick(&self, keys: &[&str]) -> State

Create a new State containing only the specified keys.

Source

pub fn merge(&self, other: &State)

Merge another state into this one (other’s values overwrite on conflict).

Source

pub fn rename(&self, from: &str, to: &str)

Rename a key.

Source

pub fn is_tracking_delta(&self) -> bool

Whether delta tracking is enabled.

Source

pub fn has_delta(&self) -> bool

Whether there are uncommitted delta changes.

Source

pub fn delta(&self) -> HashMap<String, Value>

Get a snapshot of the current delta.

Source

pub fn commit(&self)

Commit delta changes into the inner store, then clear the delta.

Source

pub fn rollback(&self)

Discard all uncommitted delta changes.

Source

pub fn app(&self) -> PrefixedState<'_>

Access state with the app: prefix scope.

Source

pub fn user(&self) -> PrefixedState<'_>

Access state with the user: prefix scope.

Source

pub fn temp(&self) -> PrefixedState<'_>

Access state with the temp: prefix scope.

Source

pub fn session(&self) -> PrefixedState<'_>

Access state with the session: prefix scope (auto-tracked signals).

Source

pub fn turn(&self) -> PrefixedState<'_>

Access state with the turn: prefix scope (reset each turn).

Source

pub fn bg(&self) -> PrefixedState<'_>

Access state with the bg: prefix scope (background tasks).

Source

pub fn derived(&self) -> ReadOnlyPrefixedState<'_>

Access read-only state with the derived: prefix scope (computed vars only).

Source

pub fn snapshot_values(&self, keys: &[&str]) -> HashMap<String, Value>

Snapshot the values of specific keys. Returns HashMap of key -> current value. Used by watchers to capture state before mutations.

Source

pub fn diff_values( &self, prev: &HashMap<String, Value>, keys: &[&str], ) -> Vec<(String, Value, Value)>

Diff current state against a previous snapshot. Returns Vec of (key, old_value, new_value) for keys that changed.

Source

pub fn to_hashmap(&self) -> HashMap<String, Value>

Export all state as a HashMap (for persistence/serialization).

Source

pub fn from_hashmap(&self, map: HashMap<String, Value>)

Restore state from a HashMap (for persistence/deserialization).

Source

pub fn clear_prefix(&self, prefix: &str)

Remove all keys with the given prefix.

Trait Implementations§

Source§

impl Clone for State

Source§

fn clone(&self) -> State

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for State

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for State

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for State

§

impl !RefUnwindSafe for State

§

impl Send for State

§

impl Sync for State

§

impl Unpin for State

§

impl UnsafeUnpin for State

§

impl !UnwindSafe for State

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more