pub struct ScopedState<T> { /* private fields */ }scoped-state only.Expand description
A memory backend’s state behind a mutex, gated by a ScopeGate so a
conditional-02 SetScope can isolate it and roll it back
(docs/design/conditional.md §7).
Demonstration quality. It is correct, but it serializes every scoped method behind one gate and clones the whole value per atomic call. A production backend uses its storage engine’s transactions instead.
Replaces Arc<Mutex<Inner>>. Cloning shares the state and the gate.
lockis for async backend methods, reads included: it waits while another task’s scope is open, so no caller sees a change an atomic method may still undo. A task in the open scope passes.lock_unscopedis for sync code and seeding helpers; it never waits on the gate.openis howbegin_setopens a scope.
With no scope ever opened, as in a deployment that does not opt in to
conditional-02, the gate is always free and lock is a plain mutex
lock.
Implementations§
Source§impl<T> ScopedState<T>
impl<T> ScopedState<T>
Sourcepub fn with_gate(value: T, gate: Arc<ScopeGate>) -> Self
pub fn with_gate(value: T, gate: Arc<ScopeGate>) -> Self
value, behind gate, which other states may share.
Sourcepub fn gate(&self) -> &Arc<ScopeGate> ⓘ
pub fn gate(&self) -> &Arc<ScopeGate> ⓘ
The gate, to share with another state through
with_gate.
Sourcepub async fn lock(&self) -> ScopedGuard<'_, T>
pub async fn lock(&self) -> ScopedGuard<'_, T>
Lock the value for an async backend method.
Waits while a scope opened by another task holds the gate; passes
at once when the running task is in the open scope
(current_set_scope), or no scope is open. A poisoned mutex is
taken anyway: the value is plain data.
Sourcepub fn lock_unscoped(&self) -> ScopedGuard<'_, T>
pub fn lock_unscoped(&self) -> ScopedGuard<'_, T>
Lock the value without waiting on the gate, for sync code and seeding helpers that never run alongside a scoped method.
Sourcepub fn deferring(&self) -> bool
pub fn deferring(&self) -> bool
Whether the running task is in an open atomic scope on this state’s gate.
Per-object methods skip their cross-object checks (unique names,
cycles, single default, quotas) when it is true: the scope’s
validate runs them against the final state at commit, so a swap
that passes through a conflict still commits (§4.2).
Source§impl<T: Clone + Send + 'static> ScopedState<T>
impl<T: Clone + Send + 'static> ScopedState<T>
Sourcepub async fn open<V>(
&self,
request: SetScopeRequest<'_>,
validate: V,
) -> Box<dyn SetScope>
pub async fn open<V>( &self, request: SetScopeRequest<'_>, validate: V, ) -> Box<dyn SetScope>
Open the SetScope for request, for
ConditionalBackend::begin_set.
Waits for the gate and records request.scope as its owner, so
calls made in that scope pass lock and every other
task waits until the scope ends. If request.scope already holds
the gate (a second state sharing it, opened for the same method),
it does not wait.
SetMode::Atomic snapshots the value. Rollback, or dropping the
scope, restores the snapshot, so the state string and change log
held in T are as before (§4.2). Commit first runs
validate(&before, &after); any failures it returns restore the
snapshot and reject the commit with CommitError::Rejected.
SetMode::Isolated takes no snapshot and never calls validate:
each change stands on its own (§6.1), and the dispatcher writes
nothing before the handler runs. Ending the scope only releases
the gate.