Skip to content

Commit

Permalink
fix(build): Remove ambiguity in service method call (#327)
Browse files Browse the repository at this point in the history
When calling methods on Arc<MyService> where the method is also defined on Arc (e.g. drop and clone), calling inner.#method_ident(request) will actually attempt to call the Arc method instead of the method on the service, resulting in a compile error. This change removes the ambiguity by dereferencing the inner Arc.
  • Loading branch information
yonran authored Apr 9, 2020
1 parent 70b345f commit 5d56daa
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ members = [
"tests/same_name",
"tests/wellknown",
"tests/extern_path/uuid",
"tests/ambiguous_methods",
"tests/extern_path/my_application"
]
16 changes: 16 additions & 0 deletions tests/ambiguous_methods/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "ambiguous_methods"
version = "0.1.0"
authors = ["Yonathan Randolph <yonathan@gmail.com>"]
edition = "2018"
publish = false
license = "MIT"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tonic = { path= "../../tonic" }
prost = "0.6"

[build-dependencies]
tonic-build = { path= "../../tonic-build" }
3 changes: 3 additions & 0 deletions tests/ambiguous_methods/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/ambiguous_methods.proto").unwrap();
}
19 changes: 19 additions & 0 deletions tests/ambiguous_methods/proto/ambiguous_methods.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto3";

package ambiguous_methods;

message DropReq {}
message DropResp {}

// The generated stubs can confuse drop and clone
// with the same method names from Arc,
// resulting in a compile error.
service HelloService {
rpc Drop (DropReq) returns (DropResp);
rpc Clone (DropReq) returns (DropResp);
}

service HelloStreamingService {
rpc Drop (DropReq) returns (stream DropResp);
rpc Clone (DropReq) returns (stream DropResp);
}
8 changes: 8 additions & 0 deletions tests/ambiguous_methods/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[macro_use]
extern crate tonic;

tonic::include_proto!("ambiguous_methods");

fn main() {
println!("Hello, world!");
}
8 changes: 4 additions & 4 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn generate_unary<T: Method>(
fn call(&mut self, request: tonic::Request<#request>) -> Self::Future {
let inner = self.0.clone();
let fut = async move {
inner.#method_ident(request).await
(*inner).#method_ident(request).await
};
Box::pin(fut)
}
Expand Down Expand Up @@ -326,7 +326,7 @@ fn generate_server_streaming<T: Method>(
fn call(&mut self, request: tonic::Request<#request>) -> Self::Future {
let inner = self.0.clone();
let fut = async move {
inner.#method_ident(request).await
(*inner).#method_ident(request).await

};
Box::pin(fut)
Expand Down Expand Up @@ -377,7 +377,7 @@ fn generate_client_streaming<T: Method>(
fn call(&mut self, request: tonic::Request<tonic::Streaming<#request>>) -> Self::Future {
let inner = self.0.clone();
let fut = async move {
inner.#method_ident(request).await
(*inner).#method_ident(request).await

};
Box::pin(fut)
Expand Down Expand Up @@ -432,7 +432,7 @@ fn generate_streaming<T: Method>(
fn call(&mut self, request: tonic::Request<tonic::Streaming<#request>>) -> Self::Future {
let inner = self.0.clone();
let fut = async move {
inner.#method_ident(request).await
(*inner).#method_ident(request).await
};
Box::pin(fut)
}
Expand Down

0 comments on commit 5d56daa

Please sign in to comment.