use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use crate::{Actor, ActorRef, Mailbox};
pub struct ActorRun<A: Actor> {
future: Pin<Box<dyn Future<Output = Result<A, A::Error>> + Send>>,
}
impl<A: Actor + Send + 'static> ActorRun<A> {
pub fn new(mut actor: A, mailbox_size: Option<usize>) -> (ActorRef<A>, Self) {
let (mailbox, actor_ref) = Mailbox::new(mailbox_size);
let future = Box::pin(async move {
actor.run_with(mailbox).await?;
Ok(actor)
});
(actor_ref, ActorRun { future })
}
}
impl<A: Actor + Send + 'static> Future for ActorRun<A> {
type Output = Result<A, A::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.future.as_mut().poll(cx)
}
}