Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Trouble passing a client to a function #156

Open
GeorgeHahn opened this issue Apr 11, 2019 · 2 comments
Open

Trouble passing a client to a function #156

GeorgeHahn opened this issue Apr 11, 2019 · 2 comments

Comments

@GeorgeHahn
Copy link
Contributor

Migrating discussion from here.

I'm running into a lot of trouble when trying to pass a client to a function. It looks like the accepting function needs to have very specific bounds on the client type in order for it to match the generated bounds on the client's RPC functions.

Minimal reproduction:
https://github.com/GeorgeHahn/tower-grpc/blob/pass-client-to-fn/tower-grpc-examples/src/helloworld/client.rs#L60

Generates the following client in helloworld.rs:

pub mod client {
    use ::tower_grpc::codegen::client::*;
    use super::{HelloRequest, HelloReply};

    /// The greeting service definition.
    #[derive(Debug, Clone)]
    pub struct Greeter<T> {
        inner: grpc::Grpc<T>,
    }

    impl<T> Greeter<T> {
        pub fn new(inner: T) -> Self {
            let inner = grpc::Grpc::new(inner);
            Self { inner }
        }

        pub fn poll_ready<R>(&mut self) -> futures::Poll<(), grpc::Status>
        where T: grpc::GrpcService<R>,
        {
            self.inner.poll_ready()
        }

        /// The greeting service definition.
        pub fn say_hello<R>(&mut self, request: grpc::Request<HelloRequest>) -> grpc::unary::ResponseFuture<HelloReply, T::Future, T::ResponseBody>
        where T: grpc::GrpcService<R>,
              grpc::unary::Once<HelloRequest>: grpc::Encodable<R>,
        {
            let path = http::PathAndQuery::from_static("/helloworld.Greeter/SayHello");
            self.inner.unary(request, path)
        }
    }
}

Am I thinking about this wrong? My gut feeling is that a generated client trait would help solve this cleanly.

@LucioFranco
Copy link
Member

@GeorgeHahn you probably want to do something like this https://github.com/toshi-search/Toshi/blob/master/src/cluster/rpc_server.rs#L29 This is how we type alias the client in toshi. Which then allows us to pass it around.

@seanmonstar
Copy link
Contributor

Though it is an interesting idea, if the client could generate a trait... It'd be nicer to be generic over T: Greeter than Greeter<T> with T: GrpcService<R>...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants