Skip to main content

Run

Struct Run 

Source
pub struct Run<C = Value, E = Value> { /* private fields */ }
Expand description

A run of a machine on xmachines-runtime, one chunk at a time: the Stepper of the workflow contract. It applies a chunk with one call of the runtime, which puts the run back itself when it refuses the chunk: the machine is shared and never compiled again.

The snapshot is lazy: Run::step gives what a chunk did and keeps the runtime’s step; Run::snapshot makes the snapshot of it, and the engine’s state only when asked. Stepper::apply makes the records, with the state at a checkpoint.

use std::sync::Arc;
use serde_json::{json, Value};
use xmachines_runtime::{Machine, Registry};
use xmachines_workflow::{Chunk, Options, Record, Run, Stepper};

let machine = Arc::new(Machine::<Value, Value>::from_json(
    &json!({ "initial": "a", "states": { "a": { "on": { "go": { "target": "b" } } }, "b": {} } }),
    &Registry::new(),
).unwrap());
let (mut run, opening) = Run::open(machine, Options::default()).unwrap();
assert_eq!(opening.len(), 1);
let mut records = Vec::new();
let seq = run.apply(Chunk::Batch(vec![json!({ "kind": "time", "now": 5 }), json!({ "type": "go" })]), &mut records).unwrap();
let Record::Snapshot { snapshot, .. } = &records[0] else { unreachable!() };
assert_eq!((seq, snapshot.value.clone(), snapshot.time), (1, json!("b"), 5));

Implementations§

Source§

impl<C, E> Run<C, E>
where C: Serialize + DeserializeOwned + Clone + Send + Sync + 'static, E: EventType + Send + Sync,

Source

pub fn open( machine: Arc<Machine<C, E>>, options: Options, ) -> Result<(Self, Vec<Record>), Fault>

A run of machine, started, or reopened of options.from, and its opening records: the events the root emitted as it started and its snapshot, of seq 0, or the snapshot of the checkpoint it reopened.

A start whose root fails is WORKFLOW_MACHINE_FAILED; a checkpoint with no state, or one the runtime can’t restore, WORKFLOW_INVALID_SNAPSHOT; a maxTimers or a checkpoint of no whole number of 1 or more, a start time past 2^53 - 1, or an input beside from, WORKFLOW_INVALID_INPUT.

Source

pub fn from_actor( actor: Actor<C, E>, seq: u64, checkpoints: Checkpoints, ) -> Result<Self, Fault>

A run of an actor a host started or restored itself, as one of a changelog, migrated to a newer machine: the run goes on from the actor’s state as the run of chunk seq, and its snapshots hold the state as checkpoints says. Gives no record: the host has its own of the actor. An actor whose runtime has broken is WORKFLOW_ENGINE_FAILED.

Source

pub fn step(&mut self, chunk: &Chunk<Value>) -> Result<Applied, Fault>

Applies one chunk, all or nothing, and says what it did, making no snapshot: the cheap form, for a host that makes the snapshot only when it needs one (Run::snapshot), as a partition of a broker, once per commit. A fault is as of Stepper::apply.

Source

pub fn last(&self) -> &Step

The runtime’s step after the last chunk that applied, or of the opening: the root, the time, the deadline and the work, with no snapshot made.

Source

pub fn snapshot(&self, with_state: bool) -> Snapshot

The snapshot of the last chunk that applied, or of the opening, with the engine’s state when with_state.

Source

pub fn record(&self) -> Record

The snapshot record of the last chunk that applied, with the state as the run’s checkpoint setting has it.

Source

pub fn checkpoint(&self) -> Checkpoint

The run as a checkpoint now, whatever its checkpoint setting: its latest snapshot with the engine’s state, and the number of the last chunk read, which a run reopens of (from).

Source

pub fn can(&self, event: &Value) -> bool

Whether the root would take event now: some transition handles it. It selects by guards alone, never running what a transition does. An event of a type that starts with xstate., which no caller writes, and an event a guard of throws on, are never taken. Changes nothing. A guard that reads the run’s children or history answers of the real state here, while the kit’s XState engine answers as if there were none: such a guard can answer differently in the two.

Source

pub fn actor(&self) -> &Actor<C, E>

The runtime’s actor, as a host reads it: its persisted system, by persist_view, written once per commit.

Source

pub fn logs(&self) -> &[Vec<Value>]

The argument lists of the root’s @xstate.log actions of the last chunk that applied, or of the opening: a run keeps no others.

Source

pub fn checkpoints(&self) -> Checkpoints

Which snapshots hold the engine’s state.

Source

pub fn max_timers(&self) -> usize

How many timers a time input fires at most in a chunk of control inputs alone, as the run started with it.

Source§

impl<C, E> Run<C, E>
where C: Serialize + DeserializeOwned + Clone + Send + Sync + 'static, E: EventType + Send + Sync,

Source

pub fn apply_text(&mut self, chunk: &str) -> Result<Vec<String>, String>

Stepper::apply of a chunk of JSON text (chunk.schema.json): its records, each JSON text, or its fault as JSON text. Text that is not JSON is a chunk that is no input, WORKFLOW_INVALID_INPUT, of the chunk’s seq, and the run goes on.

Source

pub fn can_text(&self, event: &str) -> bool

Run::can of an event of JSON text: false of text that is no event.

Source

pub fn checkpoint_text(&self) -> String

Run::checkpoint as JSON text.

Trait Implementations§

Source§

impl<C, E> Debug for Run<C, E>

Source§

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

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

impl<C, E> Stepper for Run<C, E>
where C: Serialize + DeserializeOwned + Clone + Send + Sync + 'static, E: EventType + Send + Sync,

Source§

type Input = Value

One input of a chunk, as the stepper reads it.
Source§

type Record = Record

What the stepper gives of a chunk.
Source§

fn apply( &mut self, chunk: Chunk<Value>, out: &mut impl Extend<Record>, ) -> Result<u64, Fault>

Applies one chunk, all or nothing, and pushes its records to out: the events it made the machine emit, then its snapshot, or its rejection. Gives the chunk’s seq. A fault is the chunk’s terminal outcome, and gave no record: the run goes on after one whose code FaultCode::is_recoverable, and gives the same fault again of every chunk after any other.
Source§

fn seq(&self) -> u64

The number of the last chunk read.

Auto Trait Implementations§

§

impl<C = Value, E = Value> !RefUnwindSafe for Run<C, E>

§

impl<C = Value, E = Value> !Sync for Run<C, E>

§

impl<C = Value, E = Value> !UnwindSafe for Run<C, E>

§

impl<C, E> Forget for Run<C, E>

§

impl<C, E> Freeze for Run<C, E>

§

impl<C, E> Move for Run<C, E>

§

impl<C, E> Send for Run<C, E>
where Arc<Machine<C, E>>: Send, Arc<dyn Logic>: Send, RawVecInner: Send, Arc<Vec<(String, Arc<dyn Logic>)>>: Send, &'static str: Send, String: Send, Arc<str>: Send, BoxRaw<str>: Send, BoxRaw<Value>: Send, BoxRaw<dyn AnySnapshot>: Send, RawTable<usize>: Send, Arc<dyn Fn() -> Result<(), String> + Send + Sync>: Send, BoxRaw<SystemError>: Send, BoxRaw<Scratch>: Send, BoxRaw<Vec<(ActorRef, Value)>>: Send,

§

impl<C, E> Unpin for Run<C, E>
where Arc<Machine<C, E>>: Unpin, Arc<dyn Logic>: Unpin, *const u8 is !null: Unpin, Arc<Vec<(String, Arc<dyn Logic>)>>: Unpin, &'static str: Unpin, String: Unpin, Arc<str>: Unpin, Box<str>: Unpin, Box<Value>: Unpin, Box<dyn AnySnapshot>: Unpin, Arc<dyn Fn() -> Result<(), String> + Send + Sync>: Unpin, Box<SystemError>: Unpin, Box<Scratch>: Unpin, Box<Vec<(ActorRef, Value)>>: Unpin,

§

impl<C, E> UnsafeUnpin for Run<C, E>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

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.