Skip to main content

SharedState

Struct SharedState 

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

A thread-safe, async, string→JSON store shared by all executors in a run.

Cheap to clone (an Arc handle); clones share the same underlying store. Guards against concurrent access with a tokio::sync::RwLock. See the module docs for the staged (pending/committed) superstep semantics.

Warning: keys beginning with _ are reserved for internal framework use.

Implementations§

Source§

impl SharedState

Source

pub fn new() -> Self

Create an empty shared state.

Source

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

Get a value by key, if present. Checks the pending buffer first (so an executor observes its own writes) and then committed state.

Source

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

Stage a value for key, overwriting any existing pending write. The value is visible to subsequent SharedState::get calls but is not folded into committed state until SharedState::commit.

Source

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

Whether a key exists in pending (as a non-tombstone) or committed state.

Source

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

Stage a deletion of key, returning whether the key currently exists.

If the key exists only in the pending buffer it is removed there; if it exists in committed state a tombstone is staged so the key is removed at SharedState::commit.

Source

pub async fn update<F>(&self, key: impl Into<String>, f: F)
where F: FnOnce(Option<Value>) -> Value,

Atomically read-modify-write a key under the write lock, staging the result into the pending buffer.

The closure receives the current value (pending-first, then committed) or None and returns the new value to stage. Rust analogue of Python’s hold/set_within_hold pattern for a single key.

Source

pub async fn commit(&self)

Fold all pending writes into committed state and clear the buffer.

Called by the runner at each superstep boundary, after the superstep’s executors finish successfully and before checkpointing.

Source

pub async fn discard(&self)

Discard all pending writes without committing them.

Source

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

Export a snapshot copy of the committed state (used for checkpointing). Pending writes are deliberately excluded.

Source

pub async fn import(&self, state: HashMap<String, Value>)

Merge a serialized state map into committed state (used on restore).

Source

pub async fn clear(&self)

Remove all entries, both committed and pending.

Trait Implementations§

Source§

impl Clone for SharedState

Source§

fn clone(&self) -> SharedState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Default for SharedState

Source§

fn default() -> SharedState

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

Auto Trait Implementations§

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> 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 = !

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<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