Skip to content

Instantly share code, notes, and snippets.

@ian-p-cooke
Created July 16, 2019 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian-p-cooke/acf066570810ad0f5e16b30a0e4b70ca to your computer and use it in GitHub Desktop.
Save ian-p-cooke/acf066570810ad0f5e16b30a0e4b70ca to your computer and use it in GitHub Desktop.
#![deny(warnings, rust_2018_idioms)]
use futures::Future;
use hyper::client::connect::{Destination, HttpConnector};
use tower_grpc::Request;
use tower_hyper::{client, util};
use tower_util::MakeService;
use std::error::Error;
pub mod hello_world {
include!(concat!(env!("OUT_DIR"), "/helloworld.rs"));
}
//What is the correct return type here?
fn make_client(uri: http::Uri) -> Result<Box<dyn Future<Item=hello_world::client::Greeter<Box<dyn tower_grpc::codegen::client::grpc::GrpcService<(), ResponseBody=(), Future=(), Error=()>>>, Error=tower_grpc::Status> + Send>, Box<dyn Error>> {
let dst = Destination::try_from_uri(uri.clone())?;
let connector = util::Connector::new(HttpConnector::new(4));
let settings = client::Builder::new().http2_only(true).clone();
let mut make_client = client::Connect::with_builder(connector, settings);
let say_hello = make_client
.make_service(dst)
.map_err(|e| panic!("connect error: {:?}", e))
.and_then(move |conn| {
use crate::hello_world::client::Greeter;
let conn = tower_request_modifier::Builder::new()
.set_origin(uri)
.build(conn)
.unwrap();
// Wait until the client is ready...
Greeter::new(Box::new(conn)).ready()
});
Ok(Box::new(say_hello))
}
pub fn main() -> Result<(), Box<dyn Error>> {
let _ = ::env_logger::init();
let uri: http::Uri = format!("http://[::1]:50051").parse()?;
let say_hello = make_client(uri)?
.and_then(|mut client| {
use crate::hello_world::HelloRequest;
client.say_hello(Request::new(HelloRequest {
name: "What is in a name?".to_string(),
}))
})
.and_then(|response| {
println!("RESPONSE = {:?}", response);
Ok(())
})
.map_err(|e| {
println!("ERR = {:?}", e);
});
tokio::run(say_hello);
Ok(())
}
// Compiling tower-grpc-examples v0.1.0 (C:\Users\ipc\CLionProjects\tower-grpc\tower-grpc-examples)
// error[E0271]: type mismatch resolving `<futures::future::and_then::AndThen<futures::future::map_err::MapErr<tower_hyper::client::connect::ConnectFuture<hyper::client::connect::Destination, _, tower_hyper::util::Connector<hyper::client::connect::http::HttpConnector>, tokio_executor::global::DefaultExecutor>, [closure@tower-grpc-examples\src/helloworld/client.rs:23:18: 23:54]>, impl futures::future::Future, [closure@tower-grpc-examples\src/helloworld/client.rs:24:19: 34:10 uri:_]> as futures::future::Future>::Item == hello_world::client::Greeter<std::boxed::Box<dyn tower_grpc::generic::client::GrpcService<(), Error = (), Future = (), ResponseBody = ()>>>`
// --> tower-grpc-examples\src/helloworld/client.rs:35:8
// |
// 35 | Ok(Box::new(say_hello))
// | ^^^^^^^^^^^^^^^^^^^ expected struct `tower_request_modifier::RequestModifier`, found trait tower_grpc::generic::client::GrpcService
// |
// = note: expected type `hello_world::client::Greeter<std::boxed::Box<tower_request_modifier::RequestModifier<tower_hyper::client::connection::Connection<_>, _>>>`
// found type `hello_world::client::Greeter<std::boxed::Box<dyn tower_grpc::generic::client::GrpcService<(), Error = (), Future = (), ResponseBody = ()>>>`
// = note: required for the cast to the object type `dyn futures::future::Future<Error = tower_grpc::status::Status, Item = hello_world::client::Greeter<std::boxed::Box<dyn tower_grpc::generic::client::GrpcService<(), Error = (), Future = (), ResponseBody = ()>>>> + std::marker::Send`
// error[E0599]: no method named `say_hello` found for type `hello_world::client::Greeter<std::boxed::Box<dyn tower_grpc::generic::client::GrpcService<(), Error = (), Future = (), ResponseBody = ()>>>` in the current scope
// --> tower-grpc-examples\src/helloworld/client.rs:47:20
// |
// 47 | client.say_hello(Request::new(HelloRequest {
// | ^^^^^^^^^
// |
// ::: C:\Users\icooke\CLionProjects\tower-grpc\target\debug\build\tower-grpc-examples-7f408b4977dc8e0d\out/helloworld.rs:19:5
// |
// 19 | pub struct Greeter<T> {
// | --------------------- method `say_hello` not found for this
// |
// = help: items from traits can only be used if the trait is implemented and in scope
// = note: the following trait defines an item `say_hello`, perhaps you need to implement it:
// candidate #1: `hello_world::server::Greeter`
// error: aborting due to 2 previous errors
// Some errors have detailed explanations: E0271, E0599.
// For more information about an error, try `rustc --explain E0271`.
// error: Could not compile `tower-grpc-examples`.
// To learn more, run the command again with --verbose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment