Skip to content

Commit 564bb21

Browse files
Burning1020abel-von
andcommitted
codegen: create immutable methods if there is no unary method
Co-authored-by: Abel Feng <fshb1988@gmail.com>
1 parent ffa803a commit 564bb21

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

compiler/src/codegen.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ impl<'a> ServiceGen<'a> {
487487
.any(|method| !matches!(method.method_type().0, MethodType::Unary))
488488
}
489489

490+
fn has_unary_method(&self) -> bool {
491+
self.methods
492+
.iter()
493+
.any(|method| matches!(method.method_type().0, MethodType::Unary))
494+
}
495+
490496
fn write_client(&self, w: &mut CodeWriter) {
491497
if async_on(self.customize, "client") {
492498
self.write_async_client(w)
@@ -589,9 +595,14 @@ impl<'a> ServiceGen<'a> {
589595
);
590596

591597
let has_stream_method = self.has_stream_method();
598+
let has_unary_method = self.has_unary_method();
592599
w.pub_fn(&s, |w| {
593600
w.write_line("let mut ret = HashMap::new();");
594-
w.write_line("let mut methods = HashMap::new();");
601+
if has_unary_method {
602+
w.write_line("let mut methods = HashMap::new();");
603+
} else {
604+
w.write_line("let methods = HashMap::new();");
605+
}
595606
if has_stream_method {
596607
w.write_line("let mut streams = HashMap::new();");
597608
} else {

ttrpc-codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ readme = "README.md"
1616
protobuf-support = "3.1.0"
1717
protobuf = { version = "2.27.1" }
1818
protobuf-codegen = "3.1.0"
19-
ttrpc-compiler = "0.6.1"
19+
ttrpc-compiler = { path = "../ttrpc-compiler" }

0 commit comments

Comments
 (0)