-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from akiradeveloper/no-send
Support ?Send
- Loading branch information
Showing
7 changed files
with
162 additions
and
7 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,12 @@ | ||
[package] | ||
name = "hello-world-no-send" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
tokio = { version = "*", features = ["full"] } | ||
tower = { version = "*", features = ["full"] } | ||
|
||
norpc = { path = "../../norpc" } |
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,38 @@ | ||
use std::rc::Rc; | ||
use tokio::sync::mpsc; | ||
use tower::Service; | ||
|
||
#[norpc::service(?Send)] | ||
trait HelloWorld { | ||
// Rc<T> is !Send | ||
fn hello(s: Rc<String>) -> Rc<String>; | ||
} | ||
|
||
#[derive(Clone)] | ||
struct HelloWorldApp; | ||
#[norpc::async_trait(?Send)] | ||
impl HelloWorld for HelloWorldApp { | ||
async fn hello(self, s: Rc<String>) -> Rc<String> { | ||
format!("Hello, {}", s).into() | ||
} | ||
} | ||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_hello_world_no_send() { | ||
let local = tokio::task::LocalSet::new(); | ||
let (tx, rx) = mpsc::unbounded_channel(); | ||
local.spawn_local(async move { | ||
let app = HelloWorldApp; | ||
let service = HelloWorldService::new(app); | ||
let server = norpc::no_send::ServerChannel::new(rx, service); | ||
server.serve().await | ||
}); | ||
local.spawn_local(async move { | ||
let chan = norpc::no_send::ClientChannel::new(tx); | ||
let mut cli = HelloWorldClient::new(chan); | ||
assert_eq!( | ||
cli.hello("World".to_owned().into()).await.unwrap(), | ||
"Hello, World".to_string().into() | ||
); | ||
}); | ||
local.await; | ||
} |
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
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,73 @@ | ||
use tokio::sync::mpsc; | ||
use tokio::sync::oneshot; | ||
use tower_service::Service; | ||
|
||
use crate::{Error, Request}; | ||
|
||
/// mpsc channel wrapper on the client-side. | ||
pub struct ClientChannel<X, Y> { | ||
tx: mpsc::UnboundedSender<Request<X, Y>>, | ||
} | ||
impl<X, Y> ClientChannel<X, Y> { | ||
pub fn new(tx: mpsc::UnboundedSender<Request<X, Y>>) -> Self { | ||
Self { tx } | ||
} | ||
} | ||
impl<X, Y> Clone for ClientChannel<X, Y> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
tx: self.tx.clone(), | ||
} | ||
} | ||
} | ||
impl<X: 'static, Y: 'static> Service<X> for ClientChannel<X, Y> { | ||
type Response = Y; | ||
type Error = Error; | ||
type Future = std::pin::Pin<Box<dyn std::future::Future<Output = Result<Y, Self::Error>>>>; | ||
|
||
fn poll_ready( | ||
&mut self, | ||
_: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Result<(), Self::Error>> { | ||
Ok(()).into() | ||
} | ||
|
||
fn call(&mut self, req: X) -> Self::Future { | ||
let tx = self.tx.clone(); | ||
Box::pin(async move { | ||
let (tx1, rx1) = oneshot::channel::<Y>(); | ||
let req = Request { | ||
inner: req, | ||
tx: tx1, | ||
}; | ||
tx.send(req).map_err(|_| Error::SendError)?; | ||
let rep = rx1.await.map_err(|_| Error::RecvError)?; | ||
Ok(rep) | ||
}) | ||
} | ||
} | ||
|
||
/// mpsc channel wrapper on the server-side. | ||
pub struct ServerChannel<Req, Svc: Service<Req>> { | ||
service: Svc, | ||
rx: mpsc::UnboundedReceiver<Request<Req, Svc::Response>>, | ||
} | ||
impl<Req: 'static, Svc: Service<Req> + 'static> ServerChannel<Req, Svc> { | ||
pub fn new(rx: mpsc::UnboundedReceiver<Request<Req, Svc::Response>>, service: Svc) -> Self { | ||
Self { service, rx } | ||
} | ||
pub async fn serve(mut self) { | ||
while let Some(Request { tx, inner }) = self.rx.recv().await { | ||
// back-pressure | ||
futures::future::poll_fn(|ctx| self.service.poll_ready(ctx)) | ||
.await | ||
.ok(); | ||
let fut = self.service.call(inner); | ||
tokio::task::spawn_local(async move { | ||
if let Ok(rep) = fut.await { | ||
tx.send(rep).ok(); | ||
} | ||
}); | ||
} | ||
} | ||
} |