Move codegen to build and remove macros

This commit is contained in:
Lucio Franco
2019-09-06 15:19:18 -04:00
parent 295e7321b9
commit d0cfafafc6
20 changed files with 916 additions and 615 deletions
-1
View File
@@ -2,7 +2,6 @@ use tonic::transport::Channel;
pub mod hello_world {
include!(concat!(env!("OUT_DIR"), "/helloworld.rs"));
tonic::client!(service = "helloworld.Greeter", proto = "self");
}
#[tokio::main]
+7 -5
View File
@@ -1,18 +1,20 @@
use tonic::{Request, Response, Status, Server};
use tonic::{Request, Response, Server, Status};
pub mod hello_world {
include!(concat!(env!("OUT_DIR"), "/helloworld.rs"));
tonic::server!(service = "helloworld.Greeter", proto = "self");
}
#[derive(Default, Clone)]
#[derive(Default)]
pub struct MyGreeter {
data: String,
}
#[tonic::server_trait]
#[tonic::async_trait]
impl hello_world::Greeter for MyGreeter {
async fn say_hello(self, request: Request<hello_world::HelloRequest>) -> Result<Response<hello_world::HelloReply>, Status> {
async fn say_hello(
&self,
request: Request<hello_world::HelloRequest>,
) -> Result<Response<hello_world::HelloReply>, Status> {
println!("Got a request: {:?}", request);
let string = &self.data;
-1
View File
@@ -6,7 +6,6 @@ use tonic::{transport::Channel, Request};
mod route_guide {
include!(concat!(env!("OUT_DIR"), "/routeguide.rs"));
tonic::client!(service = "routeguide.RouteGuide", proto = "self");
}
#[tokio::main]
+20 -12
View File
@@ -8,6 +8,7 @@ use std::time::Instant;
use tokio::sync::{mpsc, Lock};
use tonic::transport::Server;
use tonic::{Request, Response, Status};
use std::pin::Pin;
pub mod routeguide {
include!(concat!(env!("OUT_DIR"), "/routeguide.rs"));
@@ -26,9 +27,9 @@ struct State {
notes: Lock<HashMap<Point, Vec<RouteNote>>>,
}
#[tonic::server(service = "routeguide.RouteGuide", proto = "routeguide")]
impl RouteGuide {
pub async fn get_feature(&self, request: Request<Point>) -> Result<Response<Feature>, Status> {
#[tonic::async_trait]
impl routeguide::RouteGuide for RouteGuide {
async fn get_feature(&self, request: Request<Point>) -> Result<Response<Feature>, Status> {
println!("GetFeature = {:?}", request);
for feature in &self.state.features[..] {
@@ -45,10 +46,12 @@ impl RouteGuide {
Ok(response)
}
pub async fn list_features(
type ListFeaturesStream = mpsc::Receiver<Result<Feature, Status>>;
async fn list_features(
&self,
request: Request<Rectangle>,
) -> Result<Response<mpsc::Receiver<Result<Feature, Status>>>, Status> {
) -> Result<Response<Self::ListFeaturesStream>, Status> {
use std::thread;
println!("ListFeatures = {:?}", request);
@@ -71,9 +74,9 @@ impl RouteGuide {
Ok(Response::new(rx))
}
pub async fn record_route(
async fn record_route(
&self,
request: Request<impl Stream<Item = Result<Point, Status>>>,
request: Request<tonic::Streaming<Point>>,
) -> Result<Response<RouteSummary>, Status> {
println!("RecordRoute");
@@ -114,10 +117,12 @@ impl RouteGuide {
Ok(Response::new(summary))
}
pub async fn route_chat(
type RouteChatStream = Pin<Box<dyn Stream<Item = Result<RouteNote, Status>> + Send + 'static>>;
async fn route_chat(
&self,
request: Request<impl Stream<Item = Result<RouteNote, Status>> + Send + 'static>,
) -> Result<Response<impl Stream<Item = Result<RouteNote, Status>> + Send>, Status> {
request: Request<tonic::Streaming<RouteNote>>,
) -> Result<Response<Self::RouteChatStream>, Status> {
println!("RouteChat");
let stream = request.into_inner();
@@ -141,7 +146,8 @@ impl RouteGuide {
}
};
Ok(Response::new(output))
// TODO: Clean this up
Ok(Response::new(Box::pin(output) as Pin<Box<dyn Stream<Item = Result<RouteNote, Status>> + Send + 'static>>))
}
}
@@ -159,8 +165,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
},
};
let svc = routeguide::RouteGuideServer::new(route_guide);
Server::builder()
.serve(addr, RouteGuideServer::new(route_guide))
.serve(addr, svc)
.await?;
Ok(())