1use failure::{Fail};
2use futures::{Future, Async};
3use futures::sync::oneshot::Receiver;
4use tk_http::client::Error;
5use errors::BadResponse;
6
7
8#[derive(Debug)]
10pub struct ResponseFuture<T>(State<T>);
11
12#[derive(Debug)]
13pub(crate) enum State<T> {
14 Waiting(Receiver<T>),
15 Error(Option<Error>),
16}
17
18impl<T> Future for ResponseFuture<T> {
19 type Item = T;
20 type Error = Error;
21 fn poll(&mut self) -> Result<Async<T>, Error> {
22 match self.0 {
23 State::Waiting(ref mut f) => match f.poll() {
24 Ok(x) => Ok(x),
25 Err(_) => Err(Error::custom(BadResponse::Canceled.compat())),
26 },
27 State::Error(ref mut e) => {
28 Err(e.take().expect("error is not taken"))
29 }
30 }
31 }
32}
33
34pub fn not_connected<T>() -> ResponseFuture<T> {
35 ResponseFuture(State::Error(Some(
36 Error::custom(BadResponse::NotConnected.compat()))))
37}
38
39pub fn from_channel<T>(s: Receiver<T>) -> ResponseFuture<T> {
40 ResponseFuture(State::Waiting(s))
41}