Skip to content

Commit

Permalink
feat: Decouple NamedService from the transport feature (#969)
Browse files Browse the repository at this point in the history
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
olix0r and hawkw committed Jun 21, 2022
1 parent 1d2083a commit feae96c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
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 @@ -160,7 +160,7 @@ pub fn generate<T: Service>(
}
}

#transport
#named
}
}
}
Expand Down Expand Up @@ -256,30 +256,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::server::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;
}
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

0 comments on commit feae96c

Please sign in to comment.