1use std::fmt;
4use std::future::Future;
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8use futures_core::ready;
9use pin_project_lite::pin_project;
10
11use crate::error::BoxError;
12
13pin_project! {
14    pub struct ResponseFuture<F> {
18        #[pin]
19        state: ResponseState<F>,
20    }
21}
22
23pin_project! {
24    #[project = ResponseStateProj]
25    enum ResponseState<F> {
26        Called {
27            #[pin]
28            fut: F
29        },
30        MemCheckFailure{
31            e: super::error::MemCheckFailure
32        },
33    }
34}
35
36impl<F> ResponseFuture<F> {
37    pub(crate) fn called(fut: F) -> Self {
38        ResponseFuture {
39            state: ResponseState::Called { fut },
40        }
41    }
42}
43
44impl<F, T, E> Future for ResponseFuture<F>
45where
46    F: Future<Output = Result<T, E>>,
47    E: Into<BoxError>,
48{
49    type Output = Result<T, BoxError>;
50
51    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
52        match self.project().state.project() {
53            ResponseStateProj::Called { fut } => {
54                Poll::Ready(ready!(fut.poll(cx)).map_err(Into::into))
55            }
56            ResponseStateProj::MemCheckFailure { e } => {
57                let e = std::mem::take(e);
58                Poll::Ready(Err(e.into()))
59            }
60        }
61    }
62}
63
64impl<F> fmt::Debug for ResponseFuture<F>
65where
66    F: fmt::Debug,
68{
69    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70        f.write_str("ResponseFuture")
71    }
72}