Skip to main content

mm1_runnable/local/
runnable.rs

1use fnonce_actor::{ActorRunBoxed, ActorRunFnOnce};
2use mm1_common::types::Never;
3
4use crate::local::fnonce_actor;
5
6pub trait ActorRun<Ctx> {
7    fn run<'run>(self, context: &'run mut Ctx) -> impl Future<Output = Never> + Send + 'run
8    where
9        Self: Sized + 'run;
10}
11
12pub fn from_fn<F, Ctx>(fn_once: F) -> FnOnceRunnable<F>
13where
14    F: ActorRunFnOnce<Ctx>,
15{
16    FnOnceRunnable(fn_once)
17}
18
19pub fn boxed_from_fn<Ctx, F>(fn_once: F) -> BoxedRunnable<Ctx>
20where
21    F: ActorRunBoxed<Ctx> + 'static,
22    Ctx: 'static,
23{
24    BoxedRunnable(Box::new(fn_once), std::any::type_name::<F>())
25}
26
27pub struct FnOnceRunnable<F>(F);
28pub struct BoxedRunnable<Ctx>(Box<dyn fnonce_actor::ActorRunBoxed<Ctx>>, &'static str);
29
30impl<Ctx> BoxedRunnable<Ctx> {
31    pub fn func_name(&self) -> &'static str {
32        // self.1.find('<').map(|idx| &self.1[..idx]).unwrap_or(self.1)
33        self.1
34    }
35}
36
37impl<Ctx, F> ActorRun<Ctx> for FnOnceRunnable<F>
38where
39    F: fnonce_actor::ActorRunFnOnce<Ctx>,
40{
41    fn run<'run>(self, context: &'run mut Ctx) -> impl Future<Output = Never> + Send + 'run
42    where
43        Self: Sized + 'run,
44    {
45        fnonce_actor::ActorRunFnOnce::run(self.0, context)
46    }
47}
48
49impl<Ctx> ActorRun<Ctx> for BoxedRunnable<Ctx> {
50    fn run<'run>(self, context: &'run mut Ctx) -> impl Future<Output = Never> + Send + 'run
51    where
52        Self: Sized + 'run,
53    {
54        fnonce_actor::ActorRunBoxed::run(self.0, context)
55    }
56}