Skip to content

Commit

Permalink
codegen: create immutable methods if there is no unary method
Browse files Browse the repository at this point in the history
Co-authored-by: Abel Feng <[email protected]>
  • Loading branch information
Burning1020 and abel-von committed Jul 30, 2024
1 parent ffa803a commit 564bb21
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ttrpc-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

0 comments on commit 564bb21

Please sign in to comment.