mm1_node/runtime/runnable/
fnonce_actor.rs1use std::future::Future;
2use std::pin::Pin;
3
4use mm1_common::types::Never;
5
6use crate::runtime::runnable::ActorExit;
7
8pub trait ActorRunBoxed<Ctx>: Send {
9 fn run<'run>(
10 self: Box<Self>,
11 context: &'run mut Ctx,
12 ) -> Pin<Box<dyn Future<Output = Never> + Send + 'run>>
13 where
14 Self: 'run;
15}
16
17pub trait ActorRunFnOnce<Ctx> {
18 fn run<'run>(self, context: &'run mut Ctx) -> impl Future<Output = Never> + Send + 'run
19 where
20 Self: Sized + 'run;
21}
22
23pub trait ActorFunc<Ctx> {
24 fn call(self, context: &mut Ctx) -> impl Future<Output = impl ActorExit<Ctx>> + Send + '_;
25}
26
27pub trait ActorFuncInner<'a, Ctx> {
28 type Out: ActorExit<Ctx>;
29 type Fut: Future<Output = Self::Out> + Send;
30
31 fn call_inner(self, context: &'a mut Ctx) -> Self::Fut;
32}
33
34impl<Ctx, Func> ActorFunc<Ctx> for Func
35where
36 Func: Send,
37 Ctx: Send + Sync,
38 for<'a> Func: ActorFuncInner<'a, Ctx> + 'a,
39{
40 async fn call(self, context: &mut Ctx) -> impl ActorExit<Ctx> {
41 self.call_inner(context).await
42 }
43}
44
45impl<Ctx, Func> ActorRunFnOnce<Ctx> for Func
46where
47 Func: ActorFunc<Ctx> + Send,
48 Ctx: Send + Sync,
49{
50 async fn run<'run>(self, context: &'run mut Ctx) -> Never
51 where
52 Self: Sized + 'run,
53 {
54 self.call(context).await.exit(context).await
55 }
56}
57
58impl<Ctx, Func> ActorRunBoxed<Ctx> for Func
59where
60 Func: ActorFunc<Ctx> + Send,
61 Ctx: Send + Sync,
62{
63 fn run<'run>(
64 self: Box<Self>,
65 context: &'run mut Ctx,
66 ) -> Pin<Box<dyn Future<Output = Never> + Send + 'run>>
67 where
68 Self: 'run,
69 {
70 Box::pin(async move { self.call(context).await.exit(context).await })
71 }
72}
73
74impl<'a, Ctx, Func, Fut> ActorFuncInner<'a, Ctx> for Func
75where
76 Ctx: 'a,
77 Func: FnOnce(&'a mut Ctx) -> Fut,
78 Fut: Future + Send + 'a,
79 Fut::Output: ActorExit<Ctx>,
80{
81 type Fut = Fut;
82 type Out = Fut::Output;
83
84 fn call_inner(self, context: &'a mut Ctx) -> Self::Fut {
85 (self)(context)
86 }
87}
88
89macro_rules! impl_actor_func_with_args {
90 ( $( $t:ident ),* $(,)? ) => {
91 impl<'a, Ctx, Func, Fut,
92 $( $t , )*
93 > ActorFuncInner<'a, Ctx> for (Func, (
94 $( $t , )*
95 ))
96 where
97 Ctx: 'a,
98 Func: FnOnce(
99 &'a mut Ctx,
100 $( $t , )*
101 ) -> Fut,
102 Fut: Future + Send + 'a,
103 Fut::Output: ActorExit<Ctx>,
104 {
105 type Fut = Fut;
106 type Out = Fut::Output;
107
108 fn call_inner(self, context: &'a mut Ctx) -> Self::Fut {
109 #[allow(non_snake_case)]
110 let (f, (
111 $( $t , )*
112 )) = self;
113 (f)(
114 context,
115 $( $t , )*
116 )
117 }
118 }
119 };
120}
121
122impl_actor_func_with_args!(T0);
123impl_actor_func_with_args!(T0, T1);
124impl_actor_func_with_args!(T0, T1, T2);
125impl_actor_func_with_args!(T0, T1, T2, T3);
126impl_actor_func_with_args!(T0, T1, T2, T3, T4);
127impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6);
128impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7);
129impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8);
130impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9);
131impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10);
132impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11);
133impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11, T12);
134impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11, T12, T13);
135impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
136impl_actor_func_with_args!(T0, T1, T2, T3, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);