1 //! Handler future types.
2 
3 use crate::response::Response;
4 use futures_util::future::Map;
5 use http::Request;
6 use pin_project_lite::pin_project;
7 use std::{convert::Infallible, future::Future, pin::Pin, task::Context};
8 use tower::util::Oneshot;
9 use tower_service::Service;
10 
11 opaque_future! {
12     /// The response future for [`IntoService`](super::IntoService).
13     pub type IntoServiceFuture<F> =
14         Map<
15             F,
16             fn(Response) -> Result<Response, Infallible>,
17         >;
18 }
19 
20 pin_project! {
21     /// The response future for [`Layered`](super::Layered).
22     pub struct LayeredFuture<B, S>
23     where
24         S: Service<Request<B>>,
25     {
26         #[pin]
27         inner: Map<Oneshot<S, Request<B>>, fn(Result<S::Response, S::Error>) -> Response>,
28     }
29 }
30 
31 impl<B, S> LayeredFuture<B, S>
32 where
33     S: Service<Request<B>>,
34 {
new( inner: Map<Oneshot<S, Request<B>>, fn(Result<S::Response, S::Error>) -> Response>, ) -> Self35     pub(super) fn new(
36         inner: Map<Oneshot<S, Request<B>>, fn(Result<S::Response, S::Error>) -> Response>,
37     ) -> Self {
38         Self { inner }
39     }
40 }
41 
42 impl<B, S> Future for LayeredFuture<B, S>
43 where
44     S: Service<Request<B>>,
45 {
46     type Output = Response;
47 
48     #[inline]
poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> std::task::Poll<Self::Output>49     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> std::task::Poll<Self::Output> {
50         self.project().inner.poll(cx)
51     }
52 }
53