From aef683fe35d4c42692d19730c98de011cdb6f303 Mon Sep 17 00:00:00 2001 From: Lucio Franco Date: Fri, 9 Aug 2019 16:01:00 -0400 Subject: [PATCH] More async macro --- tonic-macros/src/lib.rs | 42 ++++++++++++++++++++++++++++++----- tonic-macros/tests/grpc.rs | 45 +++++--------------------------------- tonic/src/lib.rs | 6 ++--- 3 files changed, 44 insertions(+), 49 deletions(-) diff --git a/tonic-macros/src/lib.rs b/tonic-macros/src/lib.rs index 8101930..7a01aa7 100644 --- a/tonic-macros/src/lib.rs +++ b/tonic-macros/src/lib.rs @@ -52,13 +52,43 @@ pub fn grpc(attr: TokenStream, item: TokenStream) -> TokenStream { // } // }; - let ts = quote! { - impl tonic::GrpcInnerService> for #s { - type Response = tonic::Response<()>; + // let ts = quote! { + // impl tonic::GrpcInnerService> for #s { + // type Response = tonic::Response<()>; - fn call<'a>(&'a mut self, request: tonic::Request<()>) -> tonic::ResponseFuture<'a, Self::Response> - where Self: 'a { - Box::pin(self.#m_ident(request)) + // fn call<'a>(&'a mut self, request: tonic::Request<()>) -> tonic::ResponseFuture<'a, Self::Response> + // where Self: 'a { + // Box::pin(self.#m_ident(request)) + // } + // } + // }; + + let ts = quote! { + pub struct GrpcServer { + inner: std::sync::Arc<#s>, + } + + impl From<#s> for GrpcServer { + fn from(t: #s) -> Self { + Self { inner: std::sync::Arc::new(t) } + } + } + + impl tower_service::Service> for GrpcServer { + type Response = tonic::Response<()>; + type Error = Status; + type Future = tonic::ResponseFuture<'static, Self::Response>; + + fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> std::task::Poll> { + std::task::Poll::Ready(Ok(())) + } + + fn call(&mut self, request: tonic::Request<()>) -> tonic::ResponseFuture<'static, Self::Response> { + let inner = self.inner.clone(); + Box::pin(async move { + inner.#m_ident(request).await + }) + //self.#m_ident(request) } } }; diff --git a/tonic-macros/tests/grpc.rs b/tonic-macros/tests/grpc.rs index 2616c51..401b03a 100644 --- a/tonic-macros/tests/grpc.rs +++ b/tonic-macros/tests/grpc.rs @@ -17,7 +17,7 @@ struct MyGreeter { #[grpc(service = "proto/helloworld.proto")] impl MyGreeter { - pub async fn say_hello(&mut self, request: Request<()>) -> Result, Status> { + pub async fn say_hello(&self, request: Request<()>) -> Result, Status> { println!("Got a request: {:?}", request); let string = &self.data; @@ -33,46 +33,11 @@ impl MyGreeter { } } -use std::future::Future; -use std::task::{Poll, Context}; -pub trait Service<'a, Request> { - type Response; - type Error; - type Future: Future> + 'a; - - fn poll_ready(&'a mut self, cx: &mut Context<'_>) -> Poll>; - fn call(&'a mut self, req: Request) -> Self::Future; -} - -struct Svc { - inner: MyGreeter, -} - -impl<'a> Service<'a, tonic::Request<()>> for Svc { - type Response = tonic::Response<()>; - type Error = tonic::Status; - type Future = tonic::ResponseFuture<'a, Self::Response>; - - fn poll_ready( - &'a mut self, - _cx: &mut std::task::Context<'_>, - ) -> std::task::Poll> { - std::task::Poll::Ready(Ok(())) - } - - fn call(&'a mut self, request: tonic::Request<()>) -> Self::Future { - use tonic::GrpcInnerService; - self.inner.call(request) - } -} - #[tokio::test] async fn grpc() { - let greeter = MyGreeter { - data: "some data".into(), - }; + let svc = MyGreeter::default(); + let mut server = GrpcServer::from(svc); - let mut svc = Svc { inner: greeter }; - - svc.call(Request::new(())).await.unwrap(); + use tower_service::Service; + server.call(tonic::Request::new(())).await.unwrap(); } diff --git a/tonic/src/lib.rs b/tonic/src/lib.rs index 1e5e4e4..a738c9b 100644 --- a/tonic/src/lib.rs +++ b/tonic/src/lib.rs @@ -2,13 +2,13 @@ pub use tower_grpc::*; use std::future::Future; use std::pin::Pin; +use std::sync::Arc; pub type ResponseFuture<'a, T> = Pin> + Send + 'a>>; pub trait GrpcInnerService { type Response; + type Future: Future>; - fn call<'a>(&'a mut self, request: Request) -> ResponseFuture<'a, Self::Response> - where - Self: 'a; + fn call(self: Arc, request: Request) -> Self::Future; }