tower_test/mock/
future.rs

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