compio_actor\actor/
handle.rs1use std::{
2 error::Error,
3 fmt,
4 future::Future,
5 pin::Pin,
6 task::{Context, Poll},
7};
8
9use futures_channel::oneshot;
10
11#[derive(Debug, PartialEq, Eq)]
13pub enum ActorExit<E: Send + 'static> {
14 Stopped,
16 Failed(E),
18}
19
20#[derive(Debug, PartialEq, Eq)]
22pub struct ActorHandleError;
23
24impl fmt::Display for ActorHandleError {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 f.write_str("actor worker stopped before reporting an exit")
27 }
28}
29
30impl Error for ActorHandleError {}
31
32pub struct ActorHandle<E: Send + 'static> {
34 pub(crate) result: oneshot::Receiver<Result<ActorExit<E>, ()>>,
35}
36
37impl<E: Send + 'static> Future for ActorHandle<E> {
38 type Output = Result<ActorExit<E>, ActorHandleError>;
39
40 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
41 Pin::new(&mut self.get_mut().result)
42 .poll(cx)
43 .map(|result| result.ok().and_then(Result::ok).ok_or(ActorHandleError))
44 }
45}