stepancheg / grpc-rust

Rust implementation of gRPC
MIT License
1.37k stars 124 forks source link

Dereference the Arc before calling methods on the interface #142

Closed yonran closed 5 years ago

yonran commented 5 years ago

When calling methods on Arc<MyService> where the method is also defined on Arc (e.g. drop and clone), calling handler_copy.method(o, p) will actually attempt to call the Arc method instead of the method on the service, resulting in a compiler error. This change removes the ambiguity by dereferencing the Arc.

// proto file:
message DropReq {}
message DropResp {}
service HelloService {
    rpc Drop (DropReq) returns (DropResp);
    rpc Clone (DropReq) returns (DropResp);
}
// Generated _grpc.rs file:
// server
impl HelloServiceServer {
    pub fn new_service_def<H : HelloService + 'static + Sync + Send + 'static>(handler: H) -> ::grpc::rt::ServerServiceDefinition {
        …
                        ::grpc::rt::MethodHandlerUnary::new(move |o, p| handler_copy.drop(o, p))
        // compiler error: explicit destructor calls not allowed
        // compiler error: expected 0 parameters
        // compiler error: expected struct `grpc::resp::SingleResponse`, found ()
    }
}

An alternative fix would be to use the fully qualified syntax for method calls (https://github.com/yonran/grpc-rust/commit/d5d7f4cd9b5ae029d7305e98209510c6bc24c151). I believe that this change should be good enough because H is only known to be H : HelloService + 'static + Sync + Send + 'static, and Sync and Send define no other methods.

stepancheg commented 5 years ago

Merged as aafba06b8ab1e079e0c32de2d92eb69a4bfe1436.

Thanks!