mm1_core/
actor_exit.rs

1use mm1_common::types::{AnyError, Never};
2
3use crate::context::Quit;
4
5pub trait ActorExit<Ctx>: 'static {
6    fn exit(self, context: &mut Ctx) -> impl Future<Output = Never> + Send + '_;
7}
8
9impl<Ctx> ActorExit<Ctx> for Never
10where
11    Ctx: Send,
12{
13    async fn exit(self, _context: &mut Ctx) -> Never {
14        match self {}
15    }
16}
17
18impl<Ctx> ActorExit<Ctx> for ()
19where
20    Ctx: Quit,
21{
22    fn exit(self, context: &mut Ctx) -> impl Future<Output = Never> + '_ {
23        context.quit_ok()
24    }
25}
26
27impl<Ctx, E> ActorExit<Ctx> for Result<(), E>
28where
29    E: Into<AnyError> + Send + Sync + 'static,
30    Ctx: Quit,
31{
32    fn exit(self, context: &mut Ctx) -> impl Future<Output = Never> + Send + '_ {
33        #[derive(Debug, thiserror::Error)]
34        #[error("wrapped dyn-error: {}", _0)]
35        struct AnyErrorWrapped(AnyError);
36
37        async move {
38            match self.map_err(Into::into).map_err(AnyErrorWrapped) {
39                Ok(()) => context.quit_ok().await,
40                Err(reason) => context.quit_err(reason).await,
41            }
42        }
43    }
44}
45
46impl<Ctx, E> ActorExit<Ctx> for Result<Never, E>
47where
48    E: Into<AnyError> + Send + Sync + 'static,
49    Ctx: Quit,
50{
51    fn exit(self, context: &mut Ctx) -> impl Future<Output = Never> + Send + '_ {
52        #[derive(Debug, thiserror::Error)]
53        #[error("wrapped dyn-error: {}", _0)]
54        struct AnyErrorWrapped(AnyError);
55
56        async move {
57            match self.map_err(Into::into).map_err(AnyErrorWrapped) {
58                Ok(never) => match never {},
59                Err(reason) => context.quit_err(reason).await,
60            }
61        }
62    }
63}