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: Decouple NamedService from the transport feature #969

Merged
merged 4 commits into from Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 4 additions & 14 deletions tonic-build/src/server.rs
Expand Up @@ -35,7 +35,7 @@ pub fn generate<T: Service>(
if package.is_empty() { "" } else { "." },
service.identifier()
);
let transport = generate_transport(&server_service, &server_trait, &path);
let named = generate_named(&server_service, &server_trait, &path);
let mod_attributes = attributes.for_mod(package);
let struct_attributes = attributes.for_struct(&path);

Expand Down Expand Up @@ -172,7 +172,7 @@ pub fn generate<T: Service>(
}
}

#transport
#named
}
}
}
Expand Down Expand Up @@ -268,30 +268,20 @@ fn generate_trait_methods<T: Service>(
stream
}

#[cfg(feature = "transport")]
fn generate_transport(
fn generate_named(
server_service: &syn::Ident,
server_trait: &syn::Ident,
service_name: &str,
) -> TokenStream {
let service_name = syn::LitStr::new(service_name, proc_macro2::Span::call_site());

quote! {
impl<T: #server_trait> tonic::transport::NamedService for #server_service<T> {
impl<T: #server_trait> tonic::NamedService for #server_service<T> {
const NAME: &'static str = #service_name;
}
}
}

#[cfg(not(feature = "transport"))]
fn generate_transport(
_server_service: &syn::Ident,
_server_trait: &syn::Ident,
_service_name: &str,
) -> TokenStream {
TokenStream::new()
}

fn generate_methods<T: Service>(
service: &T,
proto_path: &str,
Expand Down
9 changes: 9 additions & 0 deletions tonic/src/server/mod.rs
Expand Up @@ -15,3 +15,12 @@ pub use self::grpc::Grpc;
pub use self::service::{
ClientStreamingService, ServerStreamingService, StreamingService, UnaryService,
};

/// A trait to provide a static reference to the service's
/// name. This is used for routing service's within the router.
pub trait NamedService {
/// The `Service-Name` as described [here].
///
/// [here]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
const NAME: &'static str;
LucioFranco marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 1 addition & 9 deletions tonic/src/transport/server/mod.rs
Expand Up @@ -10,6 +10,7 @@ mod tls;
mod unix;

pub use super::service::Routes;
pub use crate::server::NamedService;
pub use conn::{Connected, TcpConnectInfo};
#[cfg(feature = "tls")]
pub use tls::ServerTlsConfig;
Expand Down Expand Up @@ -123,15 +124,6 @@ pub struct Router<L = Identity> {
routes: Routes,
}

/// A trait to provide a static reference to the service's
/// name. This is used for routing service's within the router.
pub trait NamedService {
/// The `Service-Name` as described [here].
///
/// [here]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
const NAME: &'static str;
}

impl<S: NamedService, T> NamedService for Either<S, T> {
const NAME: &'static str = S::NAME;
}
Expand Down