tower_util/optional/
future.rs

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