diff --git a/examples/routeguide-tutorial.md b/examples/routeguide-tutorial.md index 2ff278f0e..d9598ce7a 100644 --- a/examples/routeguide-tutorial.md +++ b/examples/routeguide-tutorial.md @@ -304,6 +304,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]. +This attribute is only needed for Rust 1.74 and below, as async fn in traits have been stabilized +in Rust 1.75. [cargo book]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts diff --git a/tonic-build/Cargo.toml b/tonic-build/Cargo.toml index b24db759e..2a9b58b09 100644 --- a/tonic-build/Cargo.toml +++ b/tonic-build/Cargo.toml @@ -22,10 +22,11 @@ quote = "1.0" syn = "2.0" [features] -default = ["transport", "prost"] +default = ["transport", "prost", "async_trait"] prost = ["prost-build"] cleanup-markdown = ["prost", "prost-build/cleanup-markdown"] transport = [] +async_trait = [] [package.metadata.docs.rs] all-features = true diff --git a/tonic-build/src/server.rs b/tonic-build/src/server.rs index d9ab1ad6b..3b392ef39 100644 --- a/tonic-build/src/server.rs +++ b/tonic-build/src/server.rs @@ -234,7 +234,8 @@ fn generate_trait( " Generated trait containing gRPC methods that should be implemented for use with {}Server.", service.name() )); - + + #[cfg(feature = "async_trait")] quote! { #trait_doc #[async_trait] @@ -242,6 +243,13 @@ fn generate_trait( #methods } } + #[cfg(not(feature = "async_trait"))] + quote! { + #trait_doc + pub trait #server_trait : Send + Sync + 'static { + #methods + } + } } fn generate_trait_methods( diff --git a/tonic/Cargo.toml b/tonic/Cargo.toml index 99388e357..62a381892 100644 --- a/tonic/Cargo.toml +++ b/tonic/Cargo.toml @@ -23,10 +23,11 @@ repository = "https://github.com/hyperium/tonic" version = "0.11.0" [features] -codegen = ["dep:async-trait"] +codegen = [] +async_trait = ["dep:async-trait"] gzip = ["dep:flate2"] zstd = ["dep:zstd"] -default = ["transport", "codegen", "prost"] +default = ["transport", "codegen", "async_trait", "prost"] prost = ["dep:prost"] tls = ["dep:rustls-pki-types", "dep:rustls-pemfile", "transport", "dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] tls-roots = ["tls-roots-common", "dep:rustls-native-certs"] diff --git a/tonic/src/codegen.rs b/tonic/src/codegen.rs index 0697d9e21..3149481f7 100644 --- a/tonic/src/codegen.rs +++ b/tonic/src/codegen.rs @@ -1,5 +1,6 @@ //! Codegen exports used by `tonic-build`. +#[cfg(feature = "async_trait")] pub use async_trait::async_trait; pub use tokio_stream; diff --git a/tonic/src/lib.rs b/tonic/src/lib.rs index 325c6af47..ef1af9a02 100644 --- a/tonic/src/lib.rs +++ b/tonic/src/lib.rs @@ -113,8 +113,8 @@ mod status; mod util; /// A re-export of [`async-trait`](https://docs.rs/async-trait) for use with codegen. -#[cfg(feature = "codegen")] -#[cfg_attr(docsrs, doc(cfg(feature = "codegen")))] +#[cfg(feature = "async_trait")] +#[cfg_attr(docsrs, doc(cfg(feature = "async_trait")))] pub use async_trait::async_trait; #[doc(inline)]