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<()> {
let runtime = Runtime::create("trait_test".to_string()).await?;
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(())
}