From bddc361bb188322d663631b15295b3979cb104c7 Mon Sep 17 00:00:00 2001 From: Matthijs Hofstra Date: Sat, 20 Jul 2013 03:02:38 +0200 Subject: [PATCH] Added a new method to extra::future (unwrap) + a test --- src/libextra/future.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/libextra/future.rs b/src/libextra/future.rs index 2d3da5bb96d1c..d8f21b460138e 100644 --- a/src/libextra/future.rs +++ b/src/libextra/future.rs @@ -60,6 +60,19 @@ impl Future { } } +impl Future { + /// 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 Future { pub fn get_ref<'a>(&'a mut self) -> &'a A { /*! @@ -179,6 +192,12 @@ mod test { assert_eq!(f.get(), ~"fail"); } + #[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);