Skip to content

Instantly share code, notes, and snippets.

@jimmycuadra
Last active May 11, 2017 10:18
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 jimmycuadra/57118fb32bdf002649bcd138935e2eeb to your computer and use it in GitHub Desktop.
Save jimmycuadra/57118fb32bdf002649bcd138935e2eeb to your computer and use it in GitHub Desktop.
Generic associated type constraint question
// In crate `endpoint`:
pub trait Endpoint {
type Request: TryInto<hyper::Request>;
type Response: TryFrom<hyper::Response>;
}
// In crate `client`:
pub struct Client;
pub struct Error;
// I want to express:
//
// "Implement `Service` for `Client` for any `Endpoint`, mapping the endpoint's request and
// response to the service's request and response."
//
// The goal is to be able to do:
//
// ```
// let foo_request = foo::Request::new();
// client.call(foo_request).and_then(|_| ...);
//
// let bar_request = bar::Request::new();
// client.call(bar_request).and_then(|_| ...);
// ```
//
// where `foo::Request` and `bar::Request` are `Endpoint::Request` for some type implementing
// `Endpoint`.
//
// What is the proper syntax for this? Is it even possible currently?
// The below currently results in this error, as expected:
//
// the type parameter `E` is not constrained by the impl trait, self type, or predicates
impl<E> tokio_service::Service for Client where E: endpoint::Endpoint {
type Request = E::Request;
type Response = E::Response;
type Error = Error;
type Future = Box<futures::Future<Item = Self::Request, Error = Self::Error>>;
fn call(&self, req: Self::Request) -> Self::Future {
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment