use future::{Async, EnvFuture, Poll};
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct Fuse<F> {
future: Option<F>,
}
pub fn new<F>(future: F) -> Fuse<F> {
Fuse {
future: Some(future),
}
}
impl<E: ?Sized, F: EnvFuture<E>> EnvFuture<E> for Fuse<F> {
type Item = F::Item;
type Error = F::Error;
fn poll(&mut self, env: &mut E) -> Poll<Self::Item, Self::Error> {
match self.future.as_mut().map(|f| f.poll(env)) {
None |
Some(Ok(Async::NotReady)) => Ok(Async::NotReady),
Some(ret@Ok(Async::Ready(_))) |
Some(ret@Err(_)) => {
self.future = None;
ret
}
}
}
fn cancel(&mut self, env: &mut E) {
self.future.take().map(|mut f| f.cancel(env));
}
}