mm1_node/runtime/
runnable.rs1use std::future::Future;
2
3use mm1_common::types::Never;
4
5mod actor_exit;
6mod fnonce_actor;
7
8pub use fnonce_actor::{ActorRunBoxed, ActorRunFnOnce};
9use mm1_proto_system::Runnable;
10
11use super::{ActorContext, Local};
12
13pub trait ActorRun<Ctx> {
14 fn run<'run>(self, context: &'run mut Ctx) -> impl Future<Output = Never> + Send + 'run
15 where
16 Self: Sized + 'run;
17}
18
19pub fn from_fn<F, Ctx>(fn_once: F) -> FnOnceRunnable<F>
20where
21 F: ActorRunFnOnce<Ctx>,
22{
23 FnOnceRunnable(fn_once)
24}
25
26pub fn boxed_from_fn<Ctx, F>(fn_once: F) -> BoxedRunnable<Ctx>
27where
28 F: ActorRunBoxed<Ctx> + 'static,
29 Ctx: 'static,
30{
31 BoxedRunnable(Box::new(fn_once), std::any::type_name::<F>())
32}
33
34pub struct FnOnceRunnable<F>(F);
35pub struct BoxedRunnable<Ctx>(Box<dyn fnonce_actor::ActorRunBoxed<Ctx>>, &'static str);
36
37impl Runnable for BoxedRunnable<ActorContext> {
38 type System = Local;
39
40 fn run_at(&self) -> Self::System {
41 Default::default()
42 }
43}
44
45impl<Ctx> BoxedRunnable<Ctx> {
46 pub fn func_name(&self) -> &'static str {
47 self.1
49 }
50}
51
52impl<Ctx, F> ActorRun<Ctx> for FnOnceRunnable<F>
53where
54 F: fnonce_actor::ActorRunFnOnce<Ctx>,
55{
56 fn run<'run>(self, context: &'run mut Ctx) -> impl Future<Output = Never> + Send + 'run
57 where
58 Self: Sized + 'run,
59 {
60 fnonce_actor::ActorRunFnOnce::run(self.0, context)
61 }
62}
63
64impl<Ctx> ActorRun<Ctx> for BoxedRunnable<Ctx> {
65 fn run<'run>(self, context: &'run mut Ctx) -> impl Future<Output = Never> + Send + 'run
66 where
67 Self: Sized + 'run,
68 {
69 fnonce_actor::ActorRunBoxed::run(self.0, context)
70 }
71}
72
73pub trait ActorExit<Ctx>: 'static {
74 fn exit(self, context: &mut Ctx) -> impl Future<Output = Never> + Send + '_;
75}