use std::sync::Arc;

use {Future, Wake, Tokens};

/// A future which "fuse"s an future once it's been resolved.
///
/// Normally futures can behave unpredictable once they're used after a future
/// has been resolved, but `Fuse` is always defined to return `None` from `poll`
/// after it has succeeded, and after it has succeeded all future calls to
/// `schedule` will be ignored.
pub struct Fuse<A> {
    future: Option<A>,
}

pub fn new<A: Future>(f: A) -> Fuse<A> {
    Fuse {
        future: Some(f),
    }
}

impl<A: Future> Future for Fuse<A> {
    type Item = A::Item;
    type Error = A::Error;

    fn poll(&mut self, tokens: &Tokens) -> Option<Result<A::Item, A::Error>> {
        let ret = self.future.as_mut().and_then(|f| f.poll(tokens));
        if ret.is_some() {
            self.future = None;
        }
        return ret
    }

    fn schedule(&mut self, wake: Arc<Wake>) {
        if let Some(ref mut f) = self.future {
            f.schedule(wake);
        }
    }
}

use std::sync::Arc;

use {Future, Wake, Tokens};

/// A future which "fuse"s an future once it's been resolved.
///
/// Normally futures can behave unpredictable once they're used after a future
/// has been resolved, but `Fuse` is always defined to return `None` from `poll`
/// after it has succeeded, and after it has succeeded all future calls to
/// `schedule` will be ignored.
pub struct Fuse<A> {
    future: A,
    done: bool,
}

pub fn new<A: Future>(f: A) -> Fuse<A> {
    Fuse {
        future: f,
        done: false,
    }
}

impl<A: Future> Future for Fuse<A> {
    type Item = A::Item;
    type Error = A::Error;

    fn poll(&mut self, tokens: &Tokens) -> Option<Result<A::Item, A::Error>> {
        if self.done {
            None
        } else {
            let res = self.future.poll(tokens);
            self.done = res.is_some();
            return res
        }
    }

    fn schedule(&mut self, wake: Arc<Wake>) {
        if !self.done {
            self.future.schedule(wake);
        }
    }
}

--- 
+++ 
@@ -9,14 +9,12 @@
 /// after it has succeeded, and after it has succeeded all future calls to
 /// `schedule` will be ignored.
 pub struct Fuse<A> {
-    future: A,
-    done: bool,
+    future: Option<A>,
 }
 
 pub fn new<A: Future>(f: A) -> Fuse<A> {
     Fuse {
-        future: f,
-        done: false,
+        future: Some(f),
     }
 }
 
@@ -25,18 +23,16 @@
     type Error = A::Error;
 
     fn poll(&mut self, tokens: &Tokens) -> Option<Result<A::Item, A::Error>> {
-        if self.done {
-            None
-        } else {
-            let res = self.future.poll(tokens);
-            self.done = res.is_some();
-            return res
+        let ret = self.future.as_mut().and_then(|f| f.poll(tokens));
+        if ret.is_some() {
+            self.future = None;
         }
+        return ret
     }
 
     fn schedule(&mut self, wake: Arc<Wake>) {
-        if !self.done {
-            self.future.schedule(wake);
+        if let Some(ref mut f) = self.future {
+            f.schedule(wake);
         }
     }
 }
Drop the future early in Fuse
