mm1_node/runtime/runnable/
fnonce_actor.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::future::Future;
use std::pin::Pin;

use mm1_common::types::Never;

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

pub trait ActorRunBoxed<Ctx>: Send {
    fn run<'run>(
        self: Box<Self>,
        context: &'run mut Ctx,
    ) -> Pin<Box<dyn Future<Output = Never> + Send + 'run>>
    where
        Self: 'run;
}

pub trait ActorRunFnOnce<Ctx> {
    fn run<'run>(self, context: &'run mut Ctx) -> impl Future<Output = Never> + Send + 'run
    where
        Self: Sized + 'run;
}

pub trait ActorFunc<Ctx> {
    fn call(self, context: &mut Ctx) -> impl Future<Output = impl ActorExit<Ctx>> + Send + '_;
}

pub trait ActorFuncInner<'a, Ctx> {
    type Out: ActorExit<Ctx>;
    type Fut: Future<Output = Self::Out> + Send;

    fn call_inner(self, context: &'a mut Ctx) -> Self::Fut;
}

impl<Ctx, Func> ActorFunc<Ctx> for Func
where
    Func: Send,
    Ctx: Send + Sync,
    for<'a> Func: ActorFuncInner<'a, Ctx> + 'a,
{
    async fn call(self, context: &mut Ctx) -> impl ActorExit<Ctx> {
        self.call_inner(context).await
    }
}

impl<Ctx, Func> ActorRunFnOnce<Ctx> for Func
where
    Func: ActorFunc<Ctx> + Send,
    Ctx: Send + Sync,
{
    async fn run<'run>(self, context: &'run mut Ctx) -> Never
    where
        Self: Sized + 'run,
    {
        self.call(context).await.exit(context).await
    }
}

impl<Ctx, Func> ActorRunBoxed<Ctx> for Func
where
    Func: ActorFunc<Ctx> + Send,
    Ctx: Send + Sync,
{
    fn run<'run>(
        self: Box<Self>,
        context: &'run mut Ctx,
    ) -> Pin<Box<dyn Future<Output = Never> + Send + 'run>>
    where
        Self: 'run,
    {
        Box::pin(async move { self.call(context).await.exit(context).await })
    }
}

impl<'a, Ctx, Func, Fut> ActorFuncInner<'a, Ctx> for Func
where
    Ctx: 'a,
    Func: FnOnce(&'a mut Ctx) -> Fut,
    Fut: Future + Send + 'a,
    Fut::Output: ActorExit<Ctx>,
{
    type Fut = Fut;
    type Out = Fut::Output;

    fn call_inner(self, context: &'a mut Ctx) -> Self::Fut {
        (self)(context)
    }
}

macro_rules! impl_actor_func_with_args {
    ( $( $t:ident ),* $(,)? ) => {
        impl<'a, Ctx, Func, Fut,
                $( $t , )*
            > ActorFuncInner<'a, Ctx> for (Func, (
                $( $t , )*
            ))
        where
            Ctx: 'a,
            Func: FnOnce(
                    &'a mut Ctx,
                    $( $t , )*
                ) -> Fut,
            Fut: Future + Send + 'a,
            Fut::Output: ActorExit<Ctx>,
        {
            type Fut = Fut;
            type Out = Fut::Output;

            fn call_inner(self, context: &'a mut Ctx) -> Self::Fut {
                #[allow(non_snake_case)]
                let (f, (
                        $( $t , )*
                    )) = self;
                (f)(
                    context,
                    $( $t , )*
                )
            }
        }
    };
}

impl_actor_func_with_args!(T0);
impl_actor_func_with_args!(T0, T1);
impl_actor_func_with_args!(T0, T1, T2);
impl_actor_func_with_args!(T0, T1, T2, T3);
impl_actor_func_with_args!(T0, T1, T2, T3, T4);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11, T12);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11, T12, T13);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);