rogue-runtime 0.1.0

Async RPC Runtime
Documentation
use anyhow::Result;

use rogue_runtime::prelude::*;

rpc! {
    trait Name {
        async fn name(&self) -> String;
    }

    struct Alice(InstanceId);

    impl Alice {
        pub async fn new() -> Self {
            Self(InstanceId::new())
        }
    }

    impl Identity for Alice {
        fn id(&self) -> &InstanceId {
            &self.0
        }
    }

    impl Name for Alice {
        async fn name(&self) -> String {
            "Alice".to_string()
        }
    }

    struct Bob(InstanceId);

    impl Bob {
        pub async fn new() -> Self {
            Self(InstanceId::new())
        }
    }

    impl Identity for Bob {
        fn id(&self) -> &InstanceId {
            &self.0
        }
    }

    impl Name for Bob {
        async fn name(&self) -> String {
            "Bob".to_string()
        }
    }
}

async fn greet(name: impl Name) -> String {
    format!("Hello, {}!", name.name().await)
}

#[tokio::test]
async fn test_trait() -> Result<()> {
    // Create a new runtime for the trait test
    let runtime = Runtime::create("trait_test".to_string()).await?;

    // Execute RPC calls locally
    runtime
        .execute_local(async move {
            let alice = Alice::new().await;
            let bob = Bob::new().await;
            assert_eq!(greet(alice).await, "Hello, Alice!");
            assert_eq!(greet(bob).await, "Hello, Bob!");
            Ok(())
        })
        .await?;

    Ok(())
}