feat: Decouple NamedService from the transport feature (#969)
Co-authored-by: Eliza Weisman <[email protected]>
This commit is contained in:
co-authored by
Eliza Weisman
parent
1d2083a1a6
commit
feae96c5be
@@ -35,7 +35,7 @@ pub fn generate<T: Service>(
|
|||||||
if package.is_empty() { "" } else { "." },
|
if package.is_empty() { "" } else { "." },
|
||||||
service.identifier()
|
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 mod_attributes = attributes.for_mod(package);
|
||||||
let struct_attributes = attributes.for_struct(&path);
|
let struct_attributes = attributes.for_struct(&path);
|
||||||
|
|
||||||
@@ -160,7 +160,7 @@ pub fn generate<T: Service>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#transport
|
#named
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,8 +256,7 @@ fn generate_trait_methods<T: Service>(
|
|||||||
stream
|
stream
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "transport")]
|
fn generate_named(
|
||||||
fn generate_transport(
|
|
||||||
server_service: &syn::Ident,
|
server_service: &syn::Ident,
|
||||||
server_trait: &syn::Ident,
|
server_trait: &syn::Ident,
|
||||||
service_name: &str,
|
service_name: &str,
|
||||||
@@ -265,21 +264,12 @@ fn generate_transport(
|
|||||||
let service_name = syn::LitStr::new(service_name, proc_macro2::Span::call_site());
|
let service_name = syn::LitStr::new(service_name, proc_macro2::Span::call_site());
|
||||||
|
|
||||||
quote! {
|
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;
|
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>(
|
fn generate_methods<T: Service>(
|
||||||
service: &T,
|
service: &T,
|
||||||
proto_path: &str,
|
proto_path: &str,
|
||||||
|
|||||||
@@ -15,3 +15,12 @@ pub use self::grpc::Grpc;
|
|||||||
pub use self::service::{
|
pub use self::service::{
|
||||||
ClientStreamingService, ServerStreamingService, StreamingService, UnaryService,
|
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,6 +10,7 @@ mod tls;
|
|||||||
mod unix;
|
mod unix;
|
||||||
|
|
||||||
pub use super::service::Routes;
|
pub use super::service::Routes;
|
||||||
|
pub use crate::server::NamedService;
|
||||||
pub use conn::{Connected, TcpConnectInfo};
|
pub use conn::{Connected, TcpConnectInfo};
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
pub use tls::ServerTlsConfig;
|
pub use tls::ServerTlsConfig;
|
||||||
@@ -123,15 +124,6 @@ pub struct Router<L = Identity> {
|
|||||||
routes: Routes,
|
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> {
|
impl<S: NamedService, T> NamedService for Either<S, T> {
|
||||||
const NAME: &'static str = S::NAME;
|
const NAME: &'static str = S::NAME;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user