tower/util/optional/
future.rs

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