diff --git a/tonic-build/src/client.rs b/tonic-build/src/client.rs index 913d3e7bb..4c3198484 100644 --- a/tonic-build/src/client.rs +++ b/tonic-build/src/client.rs @@ -167,7 +167,7 @@ fn generate_unary( compile_well_known_types: bool, path: String, ) -> TokenStream { - let codec_name = syn::parse_str::(T::CODEC_PATH).unwrap(); + let codec_name = syn::parse_str::(method.codec_path()).unwrap(); let ident = format_ident!("{}", method.name()); let (request, response) = method.request_response_name(proto_path, compile_well_known_types); @@ -192,7 +192,7 @@ fn generate_server_streaming( compile_well_known_types: bool, path: String, ) -> TokenStream { - let codec_name = syn::parse_str::(T::CODEC_PATH).unwrap(); + let codec_name = syn::parse_str::(method.codec_path()).unwrap(); let ident = format_ident!("{}", method.name()); let (request, response) = method.request_response_name(proto_path, compile_well_known_types); @@ -218,7 +218,7 @@ fn generate_client_streaming( compile_well_known_types: bool, path: String, ) -> TokenStream { - let codec_name = syn::parse_str::(T::CODEC_PATH).unwrap(); + let codec_name = syn::parse_str::(method.codec_path()).unwrap(); let ident = format_ident!("{}", method.name()); let (request, response) = method.request_response_name(proto_path, compile_well_known_types); @@ -244,7 +244,7 @@ fn generate_streaming( compile_well_known_types: bool, path: String, ) -> TokenStream { - let codec_name = syn::parse_str::(T::CODEC_PATH).unwrap(); + let codec_name = syn::parse_str::(method.codec_path()).unwrap(); let ident = format_ident!("{}", method.name()); let (request, response) = method.request_response_name(proto_path, compile_well_known_types); diff --git a/tonic-build/src/lib.rs b/tonic-build/src/lib.rs index 56f05d0e5..e6c11916a 100644 --- a/tonic-build/src/lib.rs +++ b/tonic-build/src/lib.rs @@ -91,9 +91,6 @@ pub mod server; /// to allow any codegen module to generate service /// abstractions. pub trait Service { - /// Path to the codec. - const CODEC_PATH: &'static str; - /// Comment type. type Comment: AsRef; @@ -119,8 +116,6 @@ pub trait Service { /// to generate abstraction implementations for /// the provided methods. pub trait Method { - /// Path to the codec. - const CODEC_PATH: &'static str; /// Comment type. type Comment: AsRef; @@ -128,6 +123,8 @@ pub trait Method { fn name(&self) -> &str; /// Identifier used to generate type name. fn identifier(&self) -> &str; + /// Path to the codec. + fn codec_path(&self) -> &str; /// Method is streamed by client. fn client_streaming(&self) -> bool; /// Method is streamed by server. diff --git a/tonic-build/src/prost.rs b/tonic-build/src/prost.rs index 750bcd677..779414dd9 100644 --- a/tonic-build/src/prost.rs +++ b/tonic-build/src/prost.rs @@ -2,9 +2,11 @@ use super::{client, server, Attributes}; use proc_macro2::TokenStream; use prost_build::{Config, Method, Service}; use quote::ToTokens; -use std::ffi::OsString; -use std::io; -use std::path::{Path, PathBuf}; +use std::{ + ffi::OsString, + io, + path::{Path, PathBuf}, +}; /// Configure `tonic-build` code generation. /// @@ -51,8 +53,6 @@ const PROST_CODEC_PATH: &str = "tonic::codec::ProstCodec"; const NON_PATH_TYPE_ALLOWLIST: &[&str] = &["()"]; impl crate::Service for Service { - const CODEC_PATH: &'static str = PROST_CODEC_PATH; - type Method = Method; type Comment = String; @@ -78,7 +78,6 @@ impl crate::Service for Service { } impl crate::Method for Method { - const CODEC_PATH: &'static str = PROST_CODEC_PATH; type Comment = String; fn name(&self) -> &str { @@ -89,6 +88,10 @@ impl crate::Method for Method { &self.proto_name } + fn codec_path(&self) -> &str { + PROST_CODEC_PATH + } + fn client_streaming(&self) -> bool { self.client_streaming } diff --git a/tonic-build/src/server.rs b/tonic-build/src/server.rs index ac432ad28..affd2db38 100644 --- a/tonic-build/src/server.rs +++ b/tonic-build/src/server.rs @@ -366,7 +366,7 @@ fn generate_unary( method_ident: Ident, server_trait: Ident, ) -> TokenStream { - let codec_name = syn::parse_str::(T::CODEC_PATH).unwrap(); + let codec_name = syn::parse_str::(method.codec_path()).unwrap(); let service_ident = quote::format_ident!("{}Svc", method.identifier()); @@ -415,7 +415,7 @@ fn generate_server_streaming( method_ident: Ident, server_trait: Ident, ) -> TokenStream { - let codec_name = syn::parse_str::(T::CODEC_PATH).unwrap(); + let codec_name = syn::parse_str::(method.codec_path()).unwrap(); let service_ident = quote::format_ident!("{}Svc", method.identifier()); @@ -470,7 +470,7 @@ fn generate_client_streaming( let service_ident = quote::format_ident!("{}Svc", method.identifier()); let (request, response) = method.request_response_name(proto_path, compile_well_known_types); - let codec_name = syn::parse_str::(T::CODEC_PATH).unwrap(); + let codec_name = syn::parse_str::(method.codec_path()).unwrap(); quote! { #[allow(non_camel_case_types)] @@ -517,7 +517,7 @@ fn generate_streaming( method_ident: Ident, server_trait: Ident, ) -> TokenStream { - let codec_name = syn::parse_str::(T::CODEC_PATH).unwrap(); + let codec_name = syn::parse_str::(method.codec_path()).unwrap(); let service_ident = quote::format_ident!("{}Svc", method.identifier());