-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow remote traits to specify default implementations of methods.
- Loading branch information
Showing
5 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use futures::join; | ||
|
||
use crate::loop_channel; | ||
|
||
// Avoid imports here to test if proc macro works without imports. | ||
|
||
#[remoc::rtc::remote] | ||
pub trait DefaultTrait: Sync { | ||
async fn value(&self) -> Result<u32, remoc::rtc::CallError>; | ||
|
||
async fn default_method(&self) -> Result<u32, remoc::rtc::CallError> { | ||
let a = 1; | ||
let b = 2; | ||
Ok(a + b) | ||
} | ||
} | ||
|
||
pub struct CounterObj { | ||
value: u32, | ||
} | ||
|
||
impl CounterObj { | ||
pub fn new() -> Self { | ||
Self { value: 0 } | ||
} | ||
} | ||
|
||
#[remoc::rtc::async_trait] | ||
impl DefaultTrait for CounterObj { | ||
async fn value(&self) -> Result<u32, remoc::rtc::CallError> { | ||
Ok(self.value) | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn simple() { | ||
use remoc::rtc::ServerRefMut; | ||
|
||
crate::init(); | ||
let ((mut a_tx, _), (_, mut b_rx)) = loop_channel::<DefaultTraitClient>().await; | ||
|
||
println!("Creating default server"); | ||
let mut counter_obj = CounterObj::new(); | ||
let (server, client) = DefaultTraitServerRefMut::new(&mut counter_obj, 1); | ||
|
||
println!("Sending default client"); | ||
a_tx.send(client).await.unwrap(); | ||
|
||
let client_task = async move { | ||
println!("Receiving default client"); | ||
let client = b_rx.recv().await.unwrap().unwrap(); | ||
|
||
println!("value: {}", client.value().await.unwrap()); | ||
assert_eq!(client.value().await.unwrap(), 0); | ||
|
||
println!("default_method: {}", client.default_method().await.unwrap()); | ||
assert_eq!(client.default_method().await.unwrap(), 3); | ||
}; | ||
|
||
join!(client_task, server.serve()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod default; | ||
mod generics; | ||
mod readonly; | ||
mod simple; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters