rogue-runtime 0.1.0

Async RPC Runtime
Documentation
use crate::{Identity, ObjectRef, Runtime, RuntimeTrait, async_trait};

#[async_trait]
pub trait AutoRegister {
    type Out;
    async fn register_instance(self, runtime: &Runtime) -> Self::Out;
}

/* base case: the concrete instance */
#[async_trait]
impl<T> AutoRegister for T
where
    T: Identity + Send + 'static,
{
    type Out = ObjectRef<T>;
    async fn register_instance(self, runtime: &Runtime) -> Self::Out {
        runtime.register_instance(self).await
    }
}

/* recursive wrappers */
#[async_trait]
impl<T> AutoRegister for Option<T>
where
    T: AutoRegister + Send,
{
    type Out = Option<T::Out>;
    async fn register_instance(self, runtime: &Runtime) -> Self::Out {
        match self {
            Some(v) => {
                let out = v.register_instance(runtime).await;
                Some(out)
            }
            None => None,
        }
    }
}

#[async_trait]
impl<T, E> AutoRegister for Result<T, E>
where
    T: AutoRegister + Send,
    E: Send,
{
    type Out = Result<T::Out, E>;
    async fn register_instance(self, runtime: &Runtime) -> Self::Out {
        match self {
            Ok(v) => {
                let out = v.register_instance(runtime).await;
                Ok(out)
            }
            Err(e) => Err(e),
        }
    }
}