Skip to content

feat(tonic): rpit future for generated server traits #2283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions examples/helloworld-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ Next up, let's implement the Greeter service we previously defined in our `.prot
#[derive(Debug, Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down Expand Up @@ -207,7 +206,6 @@ pub mod hello_world {
#[derive(Debug, Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
5 changes: 2 additions & 3 deletions examples/routeguide-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ use tokio_stream::{wrappers::ReceiverStream, Stream};
```

```rust
#[tonic::async_trait]
impl RouteGuide for RouteGuideService {
async fn get_feature(&self, _request: Request<Point>) -> Result<Response<Feature>, Status> {
unimplemented!()
Expand Down Expand Up @@ -302,8 +301,8 @@ impl RouteGuide for RouteGuideService {
}
```

**Note**: The `tonic::async_trait` attribute macro adds support for async functions in traits. It
uses [async-trait] internally. You can learn more about `async fn` in traits in the [async book].
**Note**: The stubbed traits use `impl Future<...> + Send` for the return values. It
uses RPIT internally. You can learn more about `async fn` in traits in the [async book].


[cargo book]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
Expand Down
1 change: 0 additions & 1 deletion examples/src/authentication/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type EchoResult<T> = Result<Response<T>, Status>;
#[derive(Default)]
pub struct EchoServer {}

#[tonic::async_trait]
impl pb::echo_server::Echo for EchoServer {
async fn unary_echo(&self, request: Request<EchoRequest>) -> EchoResult<EchoResponse> {
let message = request.into_inner().message;
Expand Down
1 change: 0 additions & 1 deletion examples/src/autoreload/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/blocking/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub mod hello_world {
#[derive(Debug, Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/cancellation/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/codec_buffers/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use small_buf::{
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/compression/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
2 changes: 0 additions & 2 deletions examples/src/dynamic/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type EchoResult<T> = Result<Response<T>, Status>;
#[derive(Default)]
pub struct MyEcho {}

#[tonic::async_trait]
impl Echo for MyEcho {
async fn unary_echo(&self, request: Request<EchoRequest>) -> EchoResult<EchoResponse> {
println!("Got an echo request from {:?}", request.remote_addr());
Expand All @@ -43,7 +42,6 @@ fn init_echo(args: &[String], builder: &mut RoutesBuilder) {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/dynamic_load_balance/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct EchoServer {
addr: SocketAddr,
}

#[tonic::async_trait]
impl pb::echo_server::Echo for EchoServer {
async fn unary_echo(&self, request: Request<EchoRequest>) -> EchoResult<EchoResponse> {
let message = format!("{} (from {})", request.into_inner().message, self.addr);
Expand Down
1 change: 0 additions & 1 deletion examples/src/grpc-web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/h2c/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/health/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/helloworld/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/interceptor/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/json-codec/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use hello_world::greeter_server::{Greeter, GreeterServer};
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/load_balance/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct EchoServer {
addr: SocketAddr,
}

#[tonic::async_trait]
impl pb::echo_server::Echo for EchoServer {
async fn unary_echo(&self, request: Request<EchoRequest>) -> EchoResult<EchoResponse> {
let message = format!("{} (from {})", request.into_inner().message, self.addr);
Expand Down
1 change: 0 additions & 1 deletion examples/src/mock/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
2 changes: 0 additions & 2 deletions examples/src/multiplex/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand All @@ -53,7 +52,6 @@ impl Greeter for MyGreeter {
#[derive(Default)]
pub struct MyEcho {}

#[tonic::async_trait]
impl Echo for MyEcho {
async fn unary_echo(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/reflection/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod proto {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl proto::greeter_server::Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/richer-error/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/richer-error/server_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/routeguide/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub struct RouteGuideService {
features: Arc<Vec<Feature>>,
}

#[tonic::async_trait]
impl RouteGuide for RouteGuideService {
async fn get_feature(&self, request: Request<Point>) -> Result<Response<Feature>, Status> {
println!("GetFeature = {:?}", request);
Expand Down
1 change: 0 additions & 1 deletion examples/src/streaming/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ fn match_for_io_error(err_status: &Status) -> Option<&std::io::Error> {
#[derive(Debug)]
pub struct EchoServer {}

#[tonic::async_trait]
impl pb::echo_server::Echo for EchoServer {
async fn unary_echo(&self, _: Request<EchoRequest>) -> EchoResult<EchoResponse> {
Err(Status::unimplemented("not implemented"))
Expand Down
1 change: 0 additions & 1 deletion examples/src/tls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type EchoResult<T> = Result<Response<T>, Status>;
#[derive(Default)]
pub struct EchoServer {}

#[tonic::async_trait]
impl pb::echo_server::Echo for EchoServer {
async fn unary_echo(&self, request: Request<EchoRequest>) -> EchoResult<EchoResponse> {
let conn_info = request
Expand Down
1 change: 0 additions & 1 deletion examples/src/tls_client_auth/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type EchoResult<T> = Result<Response<T>, Status>;
#[derive(Default)]
pub struct EchoServer {}

#[tonic::async_trait]
impl pb::echo_server::Echo for EchoServer {
async fn unary_echo(&self, request: Request<EchoRequest>) -> EchoResult<EchoResponse> {
let certs = request
Expand Down
1 change: 0 additions & 1 deletion examples/src/tls_rustls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ type EchoResult<T> = Result<Response<T>, Status>;
#[derive(Default)]
pub struct EchoServer {}

#[tonic::async_trait]
impl pb::echo_server::Echo for EchoServer {
async fn unary_echo(&self, request: Request<EchoRequest>) -> EchoResult<EchoResponse> {
let conn_info = request.extensions().get::<Arc<ConnInfo>>().unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/src/tower/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub mod hello_world {
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
1 change: 0 additions & 1 deletion examples/src/tracing/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use hello_world::{
#[derive(Debug, Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
#[tracing::instrument]
async fn say_hello(
Expand Down
1 change: 0 additions & 1 deletion examples/src/uds/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use hello_world::{
#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
Expand Down
3 changes: 2 additions & 1 deletion grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ authors = ["gRPC Authors"]
license = "Apache-2.0"

[dependencies]
async-trait = {version = "0.1.13"}
url = "2.5.0"
tokio = { version = "1.37.0", features = ["sync"] }
tonic = { version = "0.13.0", path = "../tonic", default-features = false, features = ["codegen"] }
tonic = { version = "0.13.0", path = "../tonic", default-features = false, features = ["codegen"] }
2 changes: 1 addition & 1 deletion grpc/src/client/name_resolution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use std::{
sync::Arc,
};

use async_trait::async_trait;
use tokio::sync::Notify;
use tonic::async_trait;
use url::Url;

use crate::attributes::Attributes;
Expand Down
2 changes: 0 additions & 2 deletions interop/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Streaming<T> = Request<tonic::Streaming<T>>;
type Stream<T> = Pin<Box<dyn tokio_stream::Stream<Item = StdResult<T, Status>> + Send + 'static>>;
type BoxFuture<T, E> = Pin<Box<dyn Future<Output = StdResult<T, E>> + Send + 'static>>;

#[tonic::async_trait]
impl pb::test_service_server::TestService for TestService {
async fn empty_call(&self, _request: Request<Empty>) -> Result<Empty> {
Ok(Response::new(Empty {}))
Expand Down Expand Up @@ -158,7 +157,6 @@ impl pb::test_service_server::TestService for TestService {
#[derive(Default)]
pub struct UnimplementedService {}

#[tonic::async_trait]
impl pb::unimplemented_service_server::UnimplementedService for UnimplementedService {
async fn unimplemented_call(&self, _req: Request<Empty>) -> Result<Empty> {
Err(Status::unimplemented(""))
Expand Down
1 change: 0 additions & 1 deletion tests/compression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ impl Svc {
}
}

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn compress_output_unary(&self, _req: Request<()>) -> Result<Response<SomeData>, Status> {
let data = [0_u8; UNCOMPRESSED_MIN_BODY_SIZE];
Expand Down
2 changes: 0 additions & 2 deletions tests/default_stubs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ tonic::include_proto!("test_default");
#[derive(Debug, Default)]
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
type ServerStreamStream = Pin<Box<dyn Stream<Item = Result<(), Status>> + Send + 'static>>;
type BidirectionalStreamStream =
Expand Down Expand Up @@ -41,7 +40,6 @@ impl test_server::Test for Svc {
}
}

#[tonic::async_trait]
impl test_default_server::TestDefault for Svc {
// Default unimplemented stubs provided here.
}
1 change: 0 additions & 1 deletion tests/integration_tests/tests/client_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use tower_http::{set_header::SetRequestHeaderLayer, trace::TraceLayer};
async fn connect_supports_standard_tower_layers() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
match req.metadata().get("x-test") {
Expand Down
1 change: 0 additions & 1 deletion tests/integration_tests/tests/complex_tower_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use tower::{layer::Layer, BoxError, Service};
async fn complex_tower_layers_work() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
unimplemented!()
Expand Down
2 changes: 0 additions & 2 deletions tests/integration_tests/tests/connect_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use tonic::{
async fn getting_connect_info() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
assert!(req.local_addr().is_some());
Expand Down Expand Up @@ -77,7 +76,6 @@ pub mod unix {

struct Svc {}

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
let conn_info = req.extensions().get::<UdsConnectInfo>().unwrap();
Expand Down
1 change: 0 additions & 1 deletion tests/integration_tests/tests/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use tonic::{

struct Svc(Arc<Mutex<Option<oneshot::Sender<()>>>>);

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _: Request<Input>) -> Result<Response<Output>, Status> {
let mut l = self.0.lock().unwrap();
Expand Down
2 changes: 0 additions & 2 deletions tests/integration_tests/tests/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ struct ExtensionValue(i32);
async fn setting_extension_from_interceptor() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
let value = req.extensions().get::<ExtensionValue>().unwrap();
Expand Down Expand Up @@ -72,7 +71,6 @@ async fn setting_extension_from_interceptor() {
async fn setting_extension_from_tower() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
let value = req.extensions().get::<ExtensionValue>().unwrap();
Expand Down
1 change: 0 additions & 1 deletion tests/integration_tests/tests/http2_keep_alive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use tonic::{Request, Response, Status};

struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _: Request<Input>) -> Result<Response<Output>, Status> {
Ok(Response::new(Output {}))
Expand Down
Loading
Loading