Skip to content

Commit feae96c

Browse files
olix0rhawkw
andauthoredJun 21, 2022
feat: Decouple NamedService from the transport feature (#969)
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
1 parent 1d2083a commit feae96c

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed
 

‎tonic-build/src/server.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn generate<T: Service>(
3535
if package.is_empty() { "" } else { "." },
3636
service.identifier()
3737
);
38-
let transport = generate_transport(&server_service, &server_trait, &path);
38+
let named = generate_named(&server_service, &server_trait, &path);
3939
let mod_attributes = attributes.for_mod(package);
4040
let struct_attributes = attributes.for_struct(&path);
4141

@@ -160,7 +160,7 @@ pub fn generate<T: Service>(
160160
}
161161
}
162162

163-
#transport
163+
#named
164164
}
165165
}
166166
}
@@ -256,30 +256,20 @@ fn generate_trait_methods<T: Service>(
256256
stream
257257
}
258258

259-
#[cfg(feature = "transport")]
260-
fn generate_transport(
259+
fn generate_named(
261260
server_service: &syn::Ident,
262261
server_trait: &syn::Ident,
263262
service_name: &str,
264263
) -> TokenStream {
265264
let service_name = syn::LitStr::new(service_name, proc_macro2::Span::call_site());
266265

267266
quote! {
268-
impl<T: #server_trait> tonic::transport::NamedService for #server_service<T> {
267+
impl<T: #server_trait> tonic::server::NamedService for #server_service<T> {
269268
const NAME: &'static str = #service_name;
270269
}
271270
}
272271
}
273272

274-
#[cfg(not(feature = "transport"))]
275-
fn generate_transport(
276-
_server_service: &syn::Ident,
277-
_server_trait: &syn::Ident,
278-
_service_name: &str,
279-
) -> TokenStream {
280-
TokenStream::new()
281-
}
282-
283273
fn generate_methods<T: Service>(
284274
service: &T,
285275
proto_path: &str,

‎tonic/src/server/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ pub use self::grpc::Grpc;
1515
pub use self::service::{
1616
ClientStreamingService, ServerStreamingService, StreamingService, UnaryService,
1717
};
18+
19+
/// A trait to provide a static reference to the service's
20+
/// name. This is used for routing service's within the router.
21+
pub trait NamedService {
22+
/// The `Service-Name` as described [here].
23+
///
24+
/// [here]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
25+
const NAME: &'static str;
26+
}

‎tonic/src/transport/server/mod.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod tls;
1010
mod unix;
1111

1212
pub use super::service::Routes;
13+
pub use crate::server::NamedService;
1314
pub use conn::{Connected, TcpConnectInfo};
1415
#[cfg(feature = "tls")]
1516
pub use tls::ServerTlsConfig;
@@ -123,15 +124,6 @@ pub struct Router<L = Identity> {
123124
routes: Routes,
124125
}
125126

126-
/// A trait to provide a static reference to the service's
127-
/// name. This is used for routing service's within the router.
128-
pub trait NamedService {
129-
/// The `Service-Name` as described [here].
130-
///
131-
/// [here]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
132-
const NAME: &'static str;
133-
}
134-
135127
impl<S: NamedService, T> NamedService for Either<S, T> {
136128
const NAME: &'static str = S::NAME;
137129
}

0 commit comments

Comments
 (0)
Please sign in to comment.