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 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));
}
}