tower/util/optional/
future.rs

1use super::error;
2use pin_project_lite::pin_project;
3use std::{
4    future::Future,
5    pin::Pin,
6    task::{Context, Poll},
7};
8
9pin_project! {
10    /// Response future returned by [`Optional`].
11    ///
12    /// [`Optional`]: crate::util::Optional
13    #[derive(Debug)]
14    pub struct ResponseFuture<T> {
15        #[pin]
16        inner: Option<T>,
17    }
18}
19
20impl<T> ResponseFuture<T> {
21    pub(crate) fn new(inner: Option<T>) -> ResponseFuture<T> {
22        ResponseFuture { inner }
23    }
24}
25
26impl<F, T, E> Future for ResponseFuture<F>
27where
28    F: Future<Output = Result<T, E>>,
29    E: Into<crate::BoxError>,
30{
31    type Output = Result<T, crate::BoxError>;
32
33    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
34        match self.project().inner.as_pin_mut() {
35            Some(inner) => inner.poll(cx).map_err(Into::into),
36            None => Poll::Ready(Err(error::None::new().into())),
37        }
38    }
39}