Last active
May 11, 2017 10:18
-
-
Save jimmycuadra/57118fb32bdf002649bcd138935e2eeb to your computer and use it in GitHub Desktop.
Generic associated type constraint question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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