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
impl State
Sourcepub fn with_delta_tracking(&self) -> State
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.
Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
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.
Sourcepub fn with<F, R>(&self, key: &str, f: F) -> Option<R>
pub fn with<F, R>(&self, key: &str, f: F) -> Option<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.
Sourcepub fn get_raw(&self, key: &str) -> Option<Value>
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.
Sourcepub fn get_key<T: DeserializeOwned>(&self, key: &StateKey<T>) -> Option<T>
pub fn get_key<T: DeserializeOwned>(&self, key: &StateKey<T>) -> Option<T>
Get a typed value using a StateKey<T>.
Sourcepub fn set_key<T: Serialize>(&self, key: &StateKey<T>, value: T)
pub fn set_key<T: Serialize>(&self, key: &StateKey<T>, value: T)
Set a typed value using a StateKey<T>.
Sourcepub fn with_key<T, F, R>(&self, key: &StateKey<T>, f: F) -> Option<R>
pub fn with_key<T, F, R>(&self, key: &StateKey<T>, f: F) -> Option<R>
Zero-copy borrow using a StateKey<T>.
Sourcepub fn set(&self, key: impl Into<String>, value: impl Serialize)
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.
Sourcepub fn set_committed(&self, key: impl Into<String>, value: impl Serialize)
pub fn set_committed(&self, key: impl Into<String>, value: impl Serialize)
Set a value directly in the committed store, bypassing delta tracking.
Sourcepub fn modify<T, F>(&self, key: &str, default: T, f: F) -> T
pub fn modify<T, F>(&self, key: &str, default: T, f: F) -> 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.
Sourcepub fn pick(&self, keys: &[&str]) -> State
pub fn pick(&self, keys: &[&str]) -> State
Create a new State containing only the specified keys.
Sourcepub fn merge(&self, other: &State)
pub fn merge(&self, other: &State)
Merge another state into this one (other’s values overwrite on conflict).
Sourcepub fn is_tracking_delta(&self) -> bool
pub fn is_tracking_delta(&self) -> bool
Whether delta tracking is enabled.
Sourcepub fn app(&self) -> PrefixedState<'_>
pub fn app(&self) -> PrefixedState<'_>
Access state with the app: prefix scope.
Sourcepub fn user(&self) -> PrefixedState<'_>
pub fn user(&self) -> PrefixedState<'_>
Access state with the user: prefix scope.
Sourcepub fn temp(&self) -> PrefixedState<'_>
pub fn temp(&self) -> PrefixedState<'_>
Access state with the temp: prefix scope.
Sourcepub fn session(&self) -> PrefixedState<'_>
pub fn session(&self) -> PrefixedState<'_>
Access state with the session: prefix scope (auto-tracked signals).
Sourcepub fn turn(&self) -> PrefixedState<'_>
pub fn turn(&self) -> PrefixedState<'_>
Access state with the turn: prefix scope (reset each turn).
Sourcepub fn bg(&self) -> PrefixedState<'_>
pub fn bg(&self) -> PrefixedState<'_>
Access state with the bg: prefix scope (background tasks).
Sourcepub fn derived(&self) -> ReadOnlyPrefixedState<'_>
pub fn derived(&self) -> ReadOnlyPrefixedState<'_>
Access read-only state with the derived: prefix scope (computed vars only).
Sourcepub fn snapshot_values(&self, keys: &[&str]) -> HashMap<String, Value>
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.
Sourcepub fn diff_values(
&self,
prev: &HashMap<String, Value>,
keys: &[&str],
) -> Vec<(String, Value, Value)>
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.
Sourcepub fn to_hashmap(&self) -> HashMap<String, Value>
pub fn to_hashmap(&self) -> HashMap<String, Value>
Export all state as a HashMap (for persistence/serialization).
Sourcepub fn from_hashmap(&self, map: HashMap<String, Value>)
pub fn from_hashmap(&self, map: HashMap<String, Value>)
Restore state from a HashMap (for persistence/deserialization).
Sourcepub fn clear_prefix(&self, prefix: &str)
pub fn clear_prefix(&self, prefix: &str)
Remove all keys with the given prefix.