rogue_runtime/traits/
auto_register.rs

1use crate::{Identity, ObjectRef, Runtime, RuntimeTrait, async_trait};
2
3#[async_trait]
4pub trait AutoRegister {
5    type Out;
6    async fn register_instance(self, runtime: &Runtime) -> Self::Out;
7}
8
9/* base case: the concrete instance */
10#[async_trait]
11impl<T> AutoRegister for T
12where
13    T: Identity + Send + 'static,
14{
15    type Out = ObjectRef<T>;
16    async fn register_instance(self, runtime: &Runtime) -> Self::Out {
17        runtime.register_instance(self).await
18    }
19}
20
21/* recursive wrappers */
22#[async_trait]
23impl<T> AutoRegister for Option<T>
24where
25    T: AutoRegister + Send,
26{
27    type Out = Option<T::Out>;
28    async fn register_instance(self, runtime: &Runtime) -> Self::Out {
29        match self {
30            Some(v) => {
31                let out = v.register_instance(runtime).await;
32                Some(out)
33            }
34            None => None,
35        }
36    }
37}
38
39#[async_trait]
40impl<T, E> AutoRegister for Result<T, E>
41where
42    T: AutoRegister + Send,
43    E: Send,
44{
45    type Out = Result<T::Out, E>;
46    async fn register_instance(self, runtime: &Runtime) -> Self::Out {
47        match self {
48            Ok(v) => {
49                let out = v.register_instance(runtime).await;
50                Ok(out)
51            }
52            Err(e) => Err(e),
53        }
54    }
55}