1 use {Future, Poll, Async};
2 
3 /// A future which "fuses" a future once it's been resolved.
4 ///
5 /// Normally futures can behave unpredictable once they're used after a future
6 /// has been resolved, but `Fuse` is always defined to return `Async::NotReady`
7 /// from `poll` after it has resolved successfully or returned an error.
8 ///
9 /// This is created by the `Future::fuse` method.
10 #[derive(Debug)]
11 #[must_use = "futures do nothing unless polled"]
12 pub struct Fuse<A: Future> {
13     future: Option<A>,
14 }
15 
new<A: Future>(f: A) -> Fuse<A>16 pub fn new<A: Future>(f: A) -> Fuse<A> {
17     Fuse {
18         future: Some(f),
19     }
20 }
21 
22 impl<A: Future> Fuse<A> {
23     /// Returns whether the underlying future has finished or not.
24     ///
25     /// If this method returns `true`, then all future calls to `poll`
26     /// are guaranteed to return `Ok(Async::NotReady)`. If this returns
27     /// false, then the underlying future has not been driven to
28     /// completion.
is_done(&self) -> bool29     pub fn is_done(&self) -> bool {
30         self.future.is_none()
31     }
32 }
33 
34 impl<A: Future> Future for Fuse<A> {
35     type Item = A::Item;
36     type Error = A::Error;
37 
poll(&mut self) -> Poll<A::Item, A::Error>38     fn poll(&mut self) -> Poll<A::Item, A::Error> {
39         let res = self.future.as_mut().map(|f| f.poll());
40         match res.unwrap_or(Ok(Async::NotReady)) {
41             res @ Ok(Async::Ready(_)) |
42             res @ Err(_) => {
43                 self.future = None;
44                 res
45             }
46             Ok(Async::NotReady) => Ok(Async::NotReady)
47         }
48     }
49 }
50