Skip to main content

Run

Struct Run 

Source
pub struct Run<F> { /* private fields */ }
Expand description

One run of an Agent, from Agent::run or Agent::resume.

A run does nothing until drive consumes it. Driving hands each event to the caller’s handler while the run itself keeps going, so a handler that waits on something the run holds lets the run release it.

A run ends with exactly one AgentEvent::RunEnded: cancelling it with cancel or a CancelHandle ends it with RunOutcome::Cancelled, and so does dropping it unfinished (though a dropped run has no consumer left to read the event).

Implementations§

Source§

impl<F> Run<F>
where F: Future<Output = Result<(), AgentError>>,

Source

pub fn checkpoints(&self) -> Checkpoints

A handle to the run’s checkpoints, usable after drive took the run.

The run holds the agent, so the embedder cannot export from it mid-run; the run records a checkpoint at every AgentEvent::Checkpoint it emits, before the event is queued, and the handle reads the newest from the event handler. An embedder persists it there, so a crash mid-run loses no completed turn.

Source

pub fn cancel(&self)

Cancels the run. Driving a cancelled run ends it at once with RunOutcome::Cancelled.

Source

pub fn cancel_handle(&self) -> CancelHandle

A handle that cancels this run after drive took it.

Source

pub async fn drive<S, B, H>( self, state: S, stop: impl Future<Output = B>, handler: impl FnMut(S, TimedEvent) -> H, ) -> Result<ControlFlow<B, S>, AgentError>
where H: Future<Output = ControlFlow<B, S>>,

Runs to the end, handing each event to handler in the order the run emitted it, one call at a time.

The run is polled together with every handler call, so it keeps making progress while the handler awaits; events it emits meanwhile queue for the next call. The run stays on the caller’s task.

state is what the handler works on, typically &mut borrows of the caller’s locals: each call takes it and returns it in ControlFlow::Continue for the next. Threading it through, rather than capturing it in an async closure, keeps the driven future Send whenever the state is. A handler returns ControlFlow::Break to stop: the run is cancelled and dropped, its queued events are discarded, and drive returns that Break. Otherwise drive returns the state in Continue once the run has finished and its every event was handled.

stop cancels the run from outside, such as on a user’s request: it is polled, ahead of the run and its next event, only while no handler call is in progress, so a call already running always finishes. Once it completes, the run is cancelled and driven to its end, so the handler still sees the run’s RunEnded with RunOutcome::Cancelled, and drive returns the value in Break. A caller with nothing to stop on passes std::future::pending.

§Errors

The run’s error, after the handler has seen every event emitted before it.

Trait Implementations§

Source§

impl<F> Debug for Run<F>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<F> !Unpin for Run<F>

§

impl<F> !UnsafeUnpin for Run<F>

§

impl<F> Forget for Run<F>

§

impl<F> Freeze for Run<F>
where Pin<Box<F>>: Freeze,

§

impl<F> Move for Run<F>

§

impl<F> RefUnwindSafe for Run<F>
where Pin<Box<F>>: RefUnwindSafe,

§

impl<F> Send for Run<F>
where Pin<Box<F>>: Send,

§

impl<F> Sync for Run<F>
where Pin<Box<F>>: Sync,

§

impl<F> UnwindSafe for Run<F>
where Pin<Box<F>>: UnwindSafe,

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

Source§

type Output = T

Should always be Self
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.
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