tower_test/mock/
future.rs1use crate::mock::error::{self, Error};
4use pin_project_lite::pin_project;
5use tokio::sync::oneshot;
6
7use std::{
8 future::Future,
9 pin::Pin,
10 task::{ready, Context, Poll},
11};
12
13pin_project! {
14 #[derive(Debug)]
16 pub struct ResponseFuture<T> {
17 #[pin]
18 rx: Option<Rx<T>>,
19 }
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}