async_actor/
actor_run.rs

1use std::{
2    future::Future,
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7use crate::{Actor, ActorRef, Mailbox};
8
9/// A future that drives an actor from start to completion.
10/// Once awaited, it will run the actor, process all messages,
11/// and eventually resolve with either the actor (on success) or an error.
12pub struct ActorRun<A: Actor> {
13    future: Pin<Box<dyn Future<Output = Result<A, A::Error>> + Send>>,
14}
15
16impl<A: Actor + Send + 'static> ActorRun<A> {
17    /// Creates a new [`ActorRef`] and [`ActorRun`] future for `actor` with optional mailbox size.
18    pub fn new(mut actor: A, mailbox_size: Option<usize>) -> (ActorRef<A>, Self) {
19        let (mailbox, actor_ref) = Mailbox::new(mailbox_size);
20
21        let future = Box::pin(async move {
22            actor.run_with(mailbox).await?;
23            Ok(actor)
24        });
25
26        (actor_ref, ActorRun { future })
27    }
28}
29
30impl<A: Actor + Send + 'static> Future for ActorRun<A> {
31    type Output = Result<A, A::Error>;
32
33    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
34        self.future.as_mut().poll(cx)
35    }
36}