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>
impl<F> Run<F>
Sourcepub fn checkpoints(&self) -> Checkpoints
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.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Cancels the run. Driving a cancelled run ends it at once with
RunOutcome::Cancelled.
Sourcepub fn cancel_handle(&self) -> CancelHandle
pub fn cancel_handle(&self) -> CancelHandle
A handle that cancels this run after drive took it.
Sourcepub 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>>,
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.