Skip to content

Instantly share code, notes, and snippets.

@dacut
Created March 22, 2021 04:27
Show Gist options
  • Save dacut/13e5736306f5d15e7751481fbcb21e60 to your computer and use it in GitHub Desktop.
Save dacut/13e5736306f5d15e7751481fbcb21e60 to your computer and use it in GitHub Desktop.
Hyper traits

Hyper wants services to implement the HttpService trait, and service makers to implement the MakeServiceRef trait. These aren't exposed outside of Hyper, however.

If we could use the HttpService trait, this is what it looks like to wrap another service:

struct WrappingService<S> {
    wrapped: S,
}

impl<S> HttpService<Body> for WrappingService<S>
where
    S: HttpService<Body, ResBody=Body, Error=Box<dyn Error + Send + Sync + 'static>>,
{
    type ResBody = Body;
    type Error = S::Error;
    type Future = S::Future;
    
    fn poll_ready(&mut self, c: &mut Context) -> Poll<Result<(), S::Error>> {
        self.s.poll_ready(c)
    }
    
    fn call(&mut self, req: Request<Body>) -> Self::Future {
        ...
        self.s.call(req)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment