mm1_node/runtime/runnable/
actor_exit.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::future::Future;

use mm1_common::types::Never;
use mm1_core::context::Quit;
use mm1_proto::AnyError;

use crate::runtime::runnable::ActorExit;

impl<Ctx> ActorExit<Ctx> for Never
where
    Ctx: Send,
{
    async fn exit(self, _context: &mut Ctx) -> Never {
        unreachable!("it's an empty type")
    }
}

impl<Ctx> ActorExit<Ctx> for ()
where
    Ctx: Quit,
{
    fn exit(self, context: &mut Ctx) -> impl Future<Output = Never> + '_ {
        context.quit_ok()
    }
}

impl<Ctx, E> ActorExit<Ctx> for Result<(), E>
where
    E: Into<AnyError> + Send + Sync + 'static,
    Ctx: Quit,
{
    fn exit(self, context: &mut Ctx) -> impl Future<Output = Never> + Send + '_ {
        #[derive(Debug, thiserror::Error)]
        #[error("wrapped dyn-error: {}", _0)]
        struct AnyErrorWrapped(AnyError);

        async move {
            match self.map_err(Into::into).map_err(AnyErrorWrapped) {
                Ok(()) => context.quit_ok().await,
                Err(reason) => context.quit_err(reason).await,
            }
        }
    }
}