Skip to content
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

feat: Make async_trait optional #1687

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions examples/routeguide-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tonic-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,22 @@ fn generate_trait<T: Service>(
" Generated trait containing gRPC methods that should be implemented for use with {}Server.",
service.name()
));


#[cfg(feature = "async_trait")]
quote! {
#trait_doc
#[async_trait]
pub trait #server_trait : Send + Sync + 'static {
#methods
}
}
#[cfg(not(feature = "async_trait"))]
quote! {
#trait_doc
pub trait #server_trait : Send + Sync + 'static {
#methods
}
}
}

fn generate_trait_methods<T: Service>(
Expand Down
5 changes: 3 additions & 2 deletions tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions tonic/src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Codegen exports used by `tonic-build`.

#[cfg(feature = "async_trait")]
pub use async_trait::async_trait;
pub use tokio_stream;

Expand Down
4 changes: 2 additions & 2 deletions tonic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down