futures_util/future/
fuse.rs

1use futures_core::{Future, Poll, Async};
2use futures_core::task;
3
4/// A future which "fuses" a future once it's been resolved.
5///
6/// Normally futures can behave unpredictable once they're used after a future
7/// has been resolved, but `Fuse` is always defined to return `Async::Pending`
8/// from `poll` after it has resolved successfully or returned an error.
9///
10/// This is created by the `Future::fuse` method.
11#[derive(Debug)]
12#[must_use = "futures do nothing unless polled"]
13pub struct Fuse<A: Future> {
14    future: Option<A>,
15}
16
17pub fn new<A: Future>(f: A) -> Fuse<A> {
18    Fuse {
19        future: Some(f),
20    }
21}
22
23impl<A: Future> Future for Fuse<A> {
24    type Item = A::Item;
25    type Error = A::Error;
26
27    fn poll(&mut self, cx: &mut task::Context) -> Poll<A::Item, A::Error> {
28        let res = self.future.as_mut().map(|f| f.poll(cx));
29        match res.unwrap_or(Ok(Async::Pending)) {
30            res @ Ok(Async::Ready(_)) |
31            res @ Err(_) => {
32                self.future = None;
33                res
34            }
35            Ok(Async::Pending) => Ok(Async::Pending)
36        }
37    }
38}