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