use std::{
error::Error,
fmt,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use futures_channel::oneshot;
#[derive(Debug, PartialEq, Eq)]
pub enum ActorExit<E: Send + 'static> {
Stopped,
Failed(E),
}
#[derive(Debug, PartialEq, Eq)]
pub struct ActorHandleError;
impl fmt::Display for ActorHandleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("actor worker stopped before reporting an exit")
}
}
impl Error for ActorHandleError {}
pub struct ActorHandle<E: Send + 'static> {
pub(crate) result: oneshot::Receiver<Result<ActorExit<E>, ()>>,
}
impl<E: Send + 'static> Future for ActorHandle<E> {
type Output = Result<ActorExit<E>, ActorHandleError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.get_mut().result)
.poll(cx)
.map(|result| result.ok().and_then(Result::ok).ok_or(ActorHandleError))
}
}