about summary refs log tree commit diff
path: root/src/libextra
diff options
context:
space:
mode:
authorMatthijs Hofstra <thiezz@gmail.com>2013-07-20 03:02:38 +0200
committerDaniel Micay <danielmicay@gmail.com>2013-07-19 21:04:33 -0400
commiteb74f0ccf60a1e8dda1afe9a3be0e8dd4fd96f9f (patch)
tree1c80a4d22820b1630617b076c212ddde942d83f5 /src/libextra
parentddd8c156c650347267e4b02938f8efb207cb2150 (diff)
downloadrust-eb74f0ccf60a1e8dda1afe9a3be0e8dd4fd96f9f.tar.gz
rust-eb74f0ccf60a1e8dda1afe9a3be0e8dd4fd96f9f.zip
Added a new method to extra::future (unwrap) + a test
Diffstat (limited to 'src/libextra')
-rw-r--r--src/libextra/future.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/libextra/future.rs b/src/libextra/future.rs
index 2d3da5bb96d..d8f21b46013 100644
--- a/src/libextra/future.rs
+++ b/src/libextra/future.rs
@@ -61,6 +61,19 @@ impl<A:Clone> Future<A> {
 }
 
 impl<A> Future<A> {
+    /// Gets the value from this future, forcing evaluation.
+    pub fn unwrap(self) -> A {
+        let mut this = self;
+        this.get_ref();
+        let state = replace(&mut this.state, Evaluating);
+        match state {
+            Forced(v) => v,
+            _ => fail!( "Logic error." ),
+        }
+    }
+}
+
+impl<A> Future<A> {
     pub fn get_ref<'a>(&'a mut self) -> &'a A {
         /*!
         * Executes the future's closure and then returns a borrowed
@@ -180,6 +193,12 @@ mod test {
     }
 
     #[test]
+    fn test_interface_unwrap() {
+        let mut f = from_value(~"fail");
+        assert_eq!(f.unwrap(), ~"fail");
+    }
+
+    #[test]
     fn test_get_ref_method() {
         let mut f = from_value(22);
         assert_eq!(*f.get_ref(), 22);