From 564bb21a01eafc97c5f92f7b3b8e29a8178352dc Mon Sep 17 00:00:00 2001 From: Zhang Tianyang Date: Tue, 30 Jul 2024 19:56:35 +0800 Subject: [PATCH] codegen: create immutable methods if there is no unary method Co-authored-by: Abel Feng --- compiler/src/codegen.rs | 13 ++++++++++++- ttrpc-codegen/Cargo.toml | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/compiler/src/codegen.rs b/compiler/src/codegen.rs index 6fd95df1..2ee34900 100644 --- a/compiler/src/codegen.rs +++ b/compiler/src/codegen.rs @@ -487,6 +487,12 @@ impl<'a> ServiceGen<'a> { .any(|method| !matches!(method.method_type().0, MethodType::Unary)) } + fn has_unary_method(&self) -> bool { + self.methods + .iter() + .any(|method| matches!(method.method_type().0, MethodType::Unary)) + } + fn write_client(&self, w: &mut CodeWriter) { if async_on(self.customize, "client") { self.write_async_client(w) @@ -589,9 +595,14 @@ impl<'a> ServiceGen<'a> { ); let has_stream_method = self.has_stream_method(); + let has_unary_method = self.has_unary_method(); w.pub_fn(&s, |w| { w.write_line("let mut ret = HashMap::new();"); - w.write_line("let mut methods = HashMap::new();"); + if has_unary_method { + w.write_line("let mut methods = HashMap::new();"); + } else { + w.write_line("let methods = HashMap::new();"); + } if has_stream_method { w.write_line("let mut streams = HashMap::new();"); } else { diff --git a/ttrpc-codegen/Cargo.toml b/ttrpc-codegen/Cargo.toml index fa0fc2e8..8d98c9c8 100644 --- a/ttrpc-codegen/Cargo.toml +++ b/ttrpc-codegen/Cargo.toml @@ -16,4 +16,4 @@ readme = "README.md" protobuf-support = "3.1.0" protobuf = { version = "2.27.1" } protobuf-codegen = "3.1.0" -ttrpc-compiler = "0.6.1" +ttrpc-compiler = { path = "../ttrpc-compiler" }