Skip to main content

Context

Struct Context 

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

Shared workflow context.

Cheaply clonable handle to the shared state. Every step receives a Context and can read/write state, emit events, and publish to the external stream.

State values are stored as JSON internally, enabling serialization for pause/resume/checkpoint functionality. Users can still use ergonomic typed accessors (set/get) as long as their types implement Serialize/DeserializeOwned.

Implementations§

Source§

impl Context

Source

pub async fn set<T>(&self, key: &str, value: T)
where T: Serialize + Send + Sync + 'static,

Store a typed value under key.

The value is serialized to JSON before storage. Overwrites any previous value stored under the same key regardless of its type.

§Panics

Panics if the value cannot be serialized to JSON. In practice this should never happen for well-formed serde types.

Source

pub async fn get<T>(&self, key: &str) -> Option<T>
where T: DeserializeOwned + Send + Sync + Clone + 'static,

Retrieve a typed value previously stored under key.

The stored JSON is deserialized back into type T. Returns None if the key does not exist or the stored JSON cannot be deserialized into T.

Source

pub async fn set_value(&self, key: &str, value: StateValue)

Store a raw StateValue directly.

Used by language bindings for polymorphic dispatch (e.g. storing platform-serialized opaque objects via StateValue::Native).

Source

pub async fn get_value(&self, key: &str) -> Option<StateValue>

Retrieve the raw StateValue stored under key.

Returns None if the key does not exist. Unlike get, this returns the value regardless of its variant.

Source

pub async fn set_bytes(&self, key: &str, data: Vec<u8>)

Store raw binary data under key.

Useful for files, images, audio, and other binary artifacts that should not be JSON-serialized.

Source

pub async fn get_bytes(&self, key: &str) -> Option<Vec<u8>>

Retrieve raw binary data previously stored under key.

Returns None if the key does not exist or the stored value is a JSON variant rather than bytes.

Source

pub async fn send_event<E>(&self, event: E)
where E: Event + Serialize,

Emit an event into the internal routing queue.

The event will be picked up by the event loop and routed to any step whose accepts list includes its event type.

Source

pub async fn write_event_to_stream<E>(&self, event: E)
where E: Event + Serialize,

Publish an event to the external broadcast stream.

Consumers that called crate::WorkflowHandler::stream_events will receive this event. Unlike send_event, this does not route the event through the internal step registry.

Source

pub async fn collect_events<E>(&self, expected_count: usize) -> Option<Vec<E>>

Accumulate events of type E until expected_count are available.

Returns Some(Vec<E>) when exactly expected_count events have been collected, or None if not enough have arrived yet.

Once the threshold is reached the internal buffer for this type is cleared automatically so a subsequent call starts fresh.

Source

pub async fn snapshot_state(&self) -> HashMap<String, StateValue>

Returns a clone of the entire state map.

Useful for checkpointing or pausing a workflow so it can be resumed later.

Source

pub async fn restore_state(&self, state: HashMap<String, StateValue>)

Replace the state map wholesale.

Used to restore state from a previous checkpoint. Any existing state is discarded.

Source

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

Returns a clone of the collected events map (serialized as JSON).

Useful for checkpointing fan-in state alongside the main state map.

Source

pub async fn restore_collected(&self, collected: HashMap<String, Vec<Value>>)

Replace the collected events map wholesale.

Used to restore fan-in state from a previous checkpoint. Any existing collected events are discarded.

Source

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

Returns a clone of the metadata map.

Useful for checkpointing metadata alongside the main state map.

Source

pub async fn run_id(&self) -> Uuid

Get the workflow run ID from metadata.

§Panics

Panics if the run_id metadata key was never set (this is always set by the workflow engine before any step executes).

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

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

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