From 3edaa424d99277f50004b313ed5d6c13be16820b Mon Sep 17 00:00:00 2001 From: John Doneth Date: Mon, 30 Sep 2019 14:05:37 -0400 Subject: [PATCH] Include proto macros (#13) --- README.md | 4 ++-- tonic-examples/src/authentication/client.rs | 2 +- tonic-examples/src/authentication/server.rs | 2 +- tonic-examples/src/helloworld/client.rs | 2 +- tonic-examples/src/helloworld/server.rs | 2 +- tonic-examples/src/load_balance/client.rs | 4 ++-- tonic-examples/src/load_balance/server.rs | 2 +- tonic-examples/src/routeguide/client.rs | 4 ++-- tonic-examples/src/routeguide/server.rs | 2 +- tonic-examples/src/tls/client.rs | 2 +- tonic-examples/src/tls/server.rs | 2 +- tonic/src/lib.rs | 1 + tonic/src/macros.rs | 17 +++++++++++++++++ 13 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 tonic/src/macros.rs diff --git a/README.md b/README.md index 9e9fdcc..a62b0a8 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ $ cargo +beta build ```rust pub mod hello_world { - include!(concat!(env!("OUT_DIR"), "/helloworld.rs")); + tonic::include_proto!("helloworld"); } use hello_world::{client::GreeterClient, HelloRequest}; @@ -81,7 +81,7 @@ async fn main() -> Result<(), Box> { use tonic::{transport::Server, Request, Response, Status}; pub mod hello_world { - include!(concat!(env!("OUT_DIR"), "/helloworld.rs")); + tonic::include_proto!("helloworld"); } use hello_world::{ diff --git a/tonic-examples/src/authentication/client.rs b/tonic-examples/src/authentication/client.rs index c1320c8..729f206 100644 --- a/tonic-examples/src/authentication/client.rs +++ b/tonic-examples/src/authentication/client.rs @@ -1,5 +1,5 @@ pub mod pb { - include!(concat!(env!("OUT_DIR"), "/grpc.examples.echo.rs")); + tonic::include_proto!("grpc.examples.echo"); } use http::header::HeaderValue; diff --git a/tonic-examples/src/authentication/server.rs b/tonic-examples/src/authentication/server.rs index a0c78d8..d63bc9c 100644 --- a/tonic-examples/src/authentication/server.rs +++ b/tonic-examples/src/authentication/server.rs @@ -1,5 +1,5 @@ pub mod pb { - include!(concat!(env!("OUT_DIR"), "/grpc.examples.echo.rs")); + tonic::include_proto!("grpc.examples.echo"); } use pb::{EchoRequest, EchoResponse}; diff --git a/tonic-examples/src/helloworld/client.rs b/tonic-examples/src/helloworld/client.rs index 804d664..4676e95 100644 --- a/tonic-examples/src/helloworld/client.rs +++ b/tonic-examples/src/helloworld/client.rs @@ -1,5 +1,5 @@ pub mod hello_world { - include!(concat!(env!("OUT_DIR"), "/helloworld.rs")); + tonic::include_proto!("helloworld"); } use hello_world::{client::GreeterClient, HelloRequest}; diff --git a/tonic-examples/src/helloworld/server.rs b/tonic-examples/src/helloworld/server.rs index 8099d78..14d61a1 100644 --- a/tonic-examples/src/helloworld/server.rs +++ b/tonic-examples/src/helloworld/server.rs @@ -1,7 +1,7 @@ use tonic::{transport::Server, Request, Response, Status}; pub mod hello_world { - include!(concat!(env!("OUT_DIR"), "/helloworld.rs")); + tonic::include_proto!("helloworld"); } use hello_world::{ diff --git a/tonic-examples/src/load_balance/client.rs b/tonic-examples/src/load_balance/client.rs index 3ab85d7..904605a 100644 --- a/tonic-examples/src/load_balance/client.rs +++ b/tonic-examples/src/load_balance/client.rs @@ -1,5 +1,5 @@ pub mod pb { - include!(concat!(env!("OUT_DIR"), "/grpc.examples.echo.rs")); + tonic::include_proto!("grpc.examples.echo"); } use pb::{client::EchoClient, EchoRequest}; @@ -15,7 +15,7 @@ async fn main() -> Result<(), Box> { let mut client = EchoClient::new(channel); - for _ in 0..12 { + for _ in 0..12usize { let request = tonic::Request::new(EchoRequest { message: "hello".into(), }); diff --git a/tonic-examples/src/load_balance/server.rs b/tonic-examples/src/load_balance/server.rs index b5f8209..fb2a3d9 100644 --- a/tonic-examples/src/load_balance/server.rs +++ b/tonic-examples/src/load_balance/server.rs @@ -1,5 +1,5 @@ pub mod pb { - include!(concat!(env!("OUT_DIR"), "/grpc.examples.echo.rs")); + tonic::include_proto!("grpc.examples.echo"); } use pb::{EchoRequest, EchoResponse}; diff --git a/tonic-examples/src/routeguide/client.rs b/tonic-examples/src/routeguide/client.rs index 019ec04..f5ebd34 100644 --- a/tonic-examples/src/routeguide/client.rs +++ b/tonic-examples/src/routeguide/client.rs @@ -4,8 +4,8 @@ use std::time::{Duration, Instant}; use tokio::timer::Interval; use tonic::Request; -mod route_guide { - include!(concat!(env!("OUT_DIR"), "/routeguide.rs")); +pub mod route_guide { + tonic::include_proto!("routeguide"); } use route_guide::client::RouteGuideClient; diff --git a/tonic-examples/src/routeguide/server.rs b/tonic-examples/src/routeguide/server.rs index cfb28e7..164ca68 100644 --- a/tonic-examples/src/routeguide/server.rs +++ b/tonic-examples/src/routeguide/server.rs @@ -11,7 +11,7 @@ use tonic::transport::Server; use tonic::{Request, Response, Status}; pub mod routeguide { - include!(concat!(env!("OUT_DIR"), "/routeguide.rs")); + tonic::include_proto!("routeguide"); } use routeguide::{server, Feature, Point, Rectangle, RouteNote, RouteSummary}; diff --git a/tonic-examples/src/tls/client.rs b/tonic-examples/src/tls/client.rs index b8b32fb..b834676 100644 --- a/tonic-examples/src/tls/client.rs +++ b/tonic-examples/src/tls/client.rs @@ -1,5 +1,5 @@ pub mod pb { - include!(concat!(env!("OUT_DIR"), "/grpc.examples.echo.rs")); + tonic::include_proto!("/grpc.examples.echo"); } use pb::{client::EchoClient, EchoRequest}; diff --git a/tonic-examples/src/tls/server.rs b/tonic-examples/src/tls/server.rs index 57652df..5713a4f 100644 --- a/tonic-examples/src/tls/server.rs +++ b/tonic-examples/src/tls/server.rs @@ -1,5 +1,5 @@ pub mod pb { - include!(concat!(env!("OUT_DIR"), "/grpc.examples.echo.rs")); + tonic::include_proto!("/grpc.examples.echo"); } use pb::{EchoRequest, EchoResponse}; diff --git a/tonic/src/lib.rs b/tonic/src/lib.rs index c592b83..a4fc813 100644 --- a/tonic/src/lib.rs +++ b/tonic/src/lib.rs @@ -77,6 +77,7 @@ pub mod server; #[cfg(feature = "transport")] pub mod transport; +mod macros; mod request; mod response; mod status; diff --git a/tonic/src/macros.rs b/tonic/src/macros.rs new file mode 100644 index 0000000..beaa020 --- /dev/null +++ b/tonic/src/macros.rs @@ -0,0 +1,17 @@ +/// Include generated proto server and client items. +/// +/// # Example +/// ```rust,no_run +/// pub mod hello_world { +/// tonic::include_proto!("helloworld"); +/// } +/// ``` +#[macro_export] +macro_rules! include_proto { + ($package: tt) => { + include!(concat!( + env!("OUT_DIR"), + concat!("/", $package, ".rs") + )); + }; +}