//! Future types use crate::{ error::{Closed, Error}, message, }; use futures::{Async, Future, Poll}; /// Future eventually completed with the response to the original request. pub struct ResponseFuture { state: ResponseState, } enum ResponseState { Failed(Option), Rx(message::Rx), Poll(T), } impl ResponseFuture where T: Future, T::Error: Into, { pub(crate) fn new(rx: message::Rx) -> Self { ResponseFuture { state: ResponseState::Rx(rx), } } pub(crate) fn failed(err: Error) -> Self { ResponseFuture { state: ResponseState::Failed(Some(err)), } } } impl Future for ResponseFuture where T: Future, T::Error: Into, { type Item = T::Item; type Error = Error; fn poll(&mut self) -> Poll { use self::ResponseState::*; loop { let fut; match self.state { Failed(ref mut e) => { return Err(e.take().expect("polled after error")); } Rx(ref mut rx) => match rx.poll() { Ok(Async::Ready(Ok(f))) => fut = f, Ok(Async::Ready(Err(e))) => return Err(e.into()), Ok(Async::NotReady) => return Ok(Async::NotReady), Err(_) => return Err(Closed::new().into()), }, Poll(ref mut fut) => { return fut.poll().map_err(Into::into); } } self.state = Poll(fut); } } }