More async macro
This commit is contained in:
+36
-6
@@ -52,13 +52,43 @@ pub fn grpc(attr: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
// }
|
// }
|
||||||
// };
|
// };
|
||||||
|
|
||||||
let ts = quote! {
|
// let ts = quote! {
|
||||||
impl tonic::GrpcInnerService<tonic::Request<()>> for #s {
|
// impl tonic::GrpcInnerService<tonic::Request<()>> for #s {
|
||||||
type Response = tonic::Response<()>;
|
// type Response = tonic::Response<()>;
|
||||||
|
|
||||||
fn call<'a>(&'a mut self, request: tonic::Request<()>) -> tonic::ResponseFuture<'a, Self::Response>
|
// fn call<'a>(&'a mut self, request: tonic::Request<()>) -> tonic::ResponseFuture<'a, Self::Response>
|
||||||
where Self: 'a {
|
// where Self: 'a {
|
||||||
Box::pin(self.#m_ident(request))
|
// 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<tonic::Request<()>> 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<Result<(), Self::Error>> {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ struct MyGreeter {
|
|||||||
|
|
||||||
#[grpc(service = "proto/helloworld.proto")]
|
#[grpc(service = "proto/helloworld.proto")]
|
||||||
impl MyGreeter {
|
impl MyGreeter {
|
||||||
pub async fn say_hello(&mut self, request: Request<()>) -> Result<Response<()>, Status> {
|
pub async fn say_hello(&self, request: Request<()>) -> Result<Response<()>, Status> {
|
||||||
println!("Got a request: {:?}", request);
|
println!("Got a request: {:?}", request);
|
||||||
|
|
||||||
let string = &self.data;
|
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<Output = Result<Self::Response, Self::Error>> + 'a;
|
|
||||||
|
|
||||||
fn poll_ready(&'a mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
|
|
||||||
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<Result<(), Self::Error>> {
|
|
||||||
std::task::Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&'a mut self, request: tonic::Request<()>) -> Self::Future {
|
|
||||||
use tonic::GrpcInnerService;
|
|
||||||
self.inner.call(request)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn grpc() {
|
async fn grpc() {
|
||||||
let greeter = MyGreeter {
|
let svc = MyGreeter::default();
|
||||||
data: "some data".into(),
|
let mut server = GrpcServer::from(svc);
|
||||||
};
|
|
||||||
|
|
||||||
let mut svc = Svc { inner: greeter };
|
use tower_service::Service;
|
||||||
|
server.call(tonic::Request::new(())).await.unwrap();
|
||||||
svc.call(Request::new(())).await.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -2,13 +2,13 @@ pub use tower_grpc::*;
|
|||||||
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub type ResponseFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, Status>> + Send + 'a>>;
|
pub type ResponseFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, Status>> + Send + 'a>>;
|
||||||
|
|
||||||
pub trait GrpcInnerService<Request> {
|
pub trait GrpcInnerService<Request> {
|
||||||
type Response;
|
type Response;
|
||||||
|
type Future: Future<Output = Result<Self::Response, Status>>;
|
||||||
|
|
||||||
fn call<'a>(&'a mut self, request: Request) -> ResponseFuture<'a, Self::Response>
|
fn call(self: Arc<Self>, request: Request) -> Self::Future;
|
||||||
where
|
|
||||||
Self: 'a;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user