tower_test/mock/
future.rs

1//! Future types
2
3use crate::mock::error::{self, Error};
4use futures_util::ready;
5use pin_project_lite::pin_project;
6use tokio::sync::oneshot;
7
8use std::{
9    future::Future,
10    pin::Pin,
11    task::{Context, Poll},
12};
13
14pin_project! {
15    /// Future of the `Mock` response.
16    #[derive(Debug)]
17    pub struct ResponseFuture<T> {
18        #[pin]
19        rx: Option<Rx<T>>,
20    }
21}
22
23type Rx<T> = oneshot::Receiver<Result<T, Error>>;
24
25impl<T> ResponseFuture<T> {
26    pub(crate) fn new(rx: Rx<T>) -> ResponseFuture<T> {
27        ResponseFuture { rx: Some(rx) }
28    }
29
30    pub(crate) fn closed() -> ResponseFuture<T> {
31        ResponseFuture { rx: None }
32    }
33}
34
35impl<T> Future for ResponseFuture<T> {
36    type Output = Result<T, Error>;
37
38    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
39        match self.project().rx.as_pin_mut() {
40            Some(rx) => match ready!(rx.poll(cx)) {
41                Ok(r) => Poll::Ready(r),
42                Err(_) => Poll::Ready(Err(error::Closed::new().into())),
43            },
44            None => Poll::Ready(Err(error::Closed::new().into())),
45        }
46    }
47}