tower_web/util/http/
future.rs

1use futures::{Future, Poll};
2use http;
3
4/// HTTP response future trait
5///
6/// A trait "alias" for `Future` where the yielded item is an `http::Response`.
7///
8/// Using `HttpFuture` in where bounds is easier than trying to use `Future`
9/// directly.
10pub trait HttpFuture: sealed::Sealed {
11    /// The HTTP response body
12    type Body;
13
14    /// Attempt to resolve the future to a final value, registering the current
15    /// task for wakeup if the value is not yet available.
16    fn poll_http(&mut self) -> Poll<http::Response<Self::Body>, ::Error>;
17
18    /// Wraps `self` with `LiftFuture`. This provides an implementation of
19    /// `Future` for `Self`.
20    fn lift(self) -> LiftFuture<Self>
21    where Self: Sized,
22    {
23        LiftFuture { inner: self }
24    }
25}
26
27/// Contains an `HttpFuture` providing an implementation of `Future`.
28#[derive(Debug)]
29pub struct LiftFuture<T> {
30    inner: T,
31}
32
33impl<T, B> HttpFuture for T
34where T: Future<Item = http::Response<B>, Error = ::Error>
35{
36    type Body = B;
37
38    fn poll_http(&mut self) -> Poll<http::Response<Self::Body>, ::Error> {
39        Future::poll(self)
40    }
41}
42
43impl<T, B> sealed::Sealed for T
44where T: Future<Item = http::Response<B>, Error = ::Error>
45{
46}
47
48impl<T: HttpFuture> Future for LiftFuture<T> {
49    type Item = http::Response<T::Body>;
50    type Error = ::Error;
51
52    fn poll(&mut self) -> Poll<Self::Item, ::Error> {
53        self.inner.poll_http()
54    }
55}
56
57/// Must be made crate public for `Either{N}` implementations.
58pub(crate) mod sealed {
59    pub trait Sealed {}
60}