diff --git a/src/librustdoc/markdown_writer.rs b/src/librustdoc/markdown_writer.rs index 193cf7be95ed0..0db8fb09491c6 100644 --- a/src/librustdoc/markdown_writer.rs +++ b/src/librustdoc/markdown_writer.rs @@ -271,7 +271,7 @@ pub fn future_writer_factory( do task::spawn { let (writer, future) = future_writer(); comm::send(writer_ch, move writer); - let s = future::get(&future); + let s = future.get(); comm::send(markdown_ch, (page, s)); } comm::recv(writer_po) diff --git a/src/libstd/future.rs b/src/libstd/future.rs index 31144740c8ade..ad9ba03f8ca38 100644 --- a/src/libstd/future.rs +++ b/src/libstd/future.rs @@ -1,8 +1,3 @@ -// NB: transitionary, de-mode-ing. -// tjc: allowing deprecated modes due to function issue. -// can re-forbid them after snapshot -#[forbid(deprecated_pattern)]; - /*! * A type representing values that may be computed concurrently and * operations for working with them. @@ -34,27 +29,37 @@ impl Future : Drop { priv enum FutureState { Pending(fn~() -> A), Evaluating, - Forced(~A) + Forced(A) } /// Methods on the `future` type impl Future { fn get() -> A { //! Get the value of the future - - get(&self) + self.with(|a| *a) } } impl Future { - fn get_ref(&self) -> &self/A { - get_ref(self) - } - - fn with(blk: fn(&A) -> B) -> B { - //! Work with the value without copying it + pure fn with(&self, blk: pure fn(&A) -> B) -> B { + match self.state { + Forced(ref v) => { return blk(v); } + Evaluating => fail ~"Recursive forcing of future!", + Pending(_) => {} + } - with(&self, blk) + // We're lying about purity here, hence unsafe. + let mut state = Evaluating; + unsafe { + self.state <-> state; + match move state { + Forced(_) | Evaluating => fail ~"Logic error.", + Pending(move f) => { + self.state = Forced(move f()); + self.with(blk) + } + } + } } } @@ -66,7 +71,7 @@ pub fn from_value(val: A) -> Future { * not block. */ - Future {state: Forced(~(move val))} + Future {state: Forced(move val)} } pub fn from_port(port: PortOne) -> @@ -120,62 +125,12 @@ pub fn spawn(blk: fn~() -> A) -> Future { return from_port(move port); } -pub fn get_ref(future: &r/Future) -> &r/A { - /*! - * Executes the future's closure and then returns a borrowed - * pointer to the result. The borrowed pointer lasts as long as - * the future. - */ - - // The unsafety here is to hide the aliases from borrowck, which - // would otherwise be concerned that someone might reassign - // `future.state` and cause the value of the future to be freed. - // But *we* know that once `future.state` is `Forced()` it will - // never become "unforced"---so we can safely return a pointer - // into the interior of the Forced() variant which will last as - // long as the future itself. - - match future.state { - Forced(ref v) => { // v here has type &A, but with a shorter lifetime. - return unsafe{ copy_lifetime(future, &**v) }; // ...extend it. - } - Evaluating => { - fail ~"Recursive forcing of future!"; - } - Pending(_) => {} - } - - let mut state = Evaluating; - state <-> future.state; - match move state { - Forced(_) | Evaluating => { - fail ~"Logic error."; - } - Pending(move f) => { - future.state = Forced(~f()); - return get_ref(future); - } - } -} - -pub fn get(future: &Future) -> A { - //! Get the value of the future - - *get_ref(future) -} - -pub fn with(future: &Future, blk: fn(&A) -> B) -> B { - //! Work with the value without copying it - - blk(get_ref(future)) -} - #[allow(non_implicitly_copyable_typarams)] pub mod test { #[test] pub fn test_from_value() { let f = from_value(~"snail"); - assert get(&f) == ~"snail"; + assert f.get() == ~"snail"; } #[test] @@ -183,13 +138,13 @@ pub mod test { let (ch, po) = oneshot::init(); send_one(move ch, ~"whale"); let f = from_port(move po); - assert get(&f) == ~"whale"; + assert f.get() == ~"whale"; } #[test] pub fn test_from_fn() { let f = from_fn(|| ~"brail"); - assert get(&f) == ~"brail"; + assert f.get() == ~"brail"; } #[test] @@ -198,24 +153,6 @@ pub mod test { assert f.get() == ~"fail"; } - #[test] - pub fn test_with() { - let f = from_value(~"nail"); - assert with(&f, |v| copy *v) == ~"nail"; - } - - #[test] - pub fn test_get_ref_method() { - let f = from_value(22); - assert *f.get_ref() == 22; - } - - #[test] - pub fn test_get_ref_fn() { - let f = from_value(22); - assert *get_ref(&f) == 22; - } - #[test] pub fn test_interface_with() { let f = from_value(~"kale"); @@ -225,7 +162,7 @@ pub mod test { #[test] pub fn test_spawn() { let f = spawn(|| ~"bale"); - assert get(&f) == ~"bale"; + assert f.get() == ~"bale"; } #[test] @@ -233,7 +170,7 @@ pub mod test { #[ignore(cfg(target_os = "win32"))] pub fn test_futurefail() { let f = spawn(|| fail); - let _x: ~str = get(&f); + let _x: ~str = f.get(); } #[test] @@ -241,7 +178,7 @@ pub mod test { let expected = ~"schlorf"; let f = do spawn |copy expected| { copy expected }; do task::spawn |move f, move expected| { - let actual = get(&f); + let actual = f.get(); assert actual == expected; } } diff --git a/src/test/bench/msgsend-ring-mutex-arcs.rs b/src/test/bench/msgsend-ring-mutex-arcs.rs index ac2fc1f5258a0..3b872d2995f85 100644 --- a/src/test/bench/msgsend-ring-mutex-arcs.rs +++ b/src/test/bench/msgsend-ring-mutex-arcs.rs @@ -97,7 +97,7 @@ fn main() { thread_ring(0, msg_per_task, option::unwrap(move num_chan), move num_port); // synchronize - for futures.each |f| { future::get(f) }; + for futures.each |f| { f.get() }; let stop = time::precise_time_s(); diff --git a/src/test/bench/msgsend-ring-pipes.rs b/src/test/bench/msgsend-ring-pipes.rs index da49b006cd9e4..b7a2fe85a061f 100644 --- a/src/test/bench/msgsend-ring-pipes.rs +++ b/src/test/bench/msgsend-ring-pipes.rs @@ -91,7 +91,7 @@ fn main() { thread_ring(0, msg_per_task, option::unwrap(move num_chan), move num_port); // synchronize - for futures.each |f| { future::get(f) }; + for futures.each |f| { f.get() }; let stop = time::precise_time_s(); diff --git a/src/test/bench/msgsend-ring-rw-arcs.rs b/src/test/bench/msgsend-ring-rw-arcs.rs index ae0099b232743..a6321e05ef2aa 100644 --- a/src/test/bench/msgsend-ring-rw-arcs.rs +++ b/src/test/bench/msgsend-ring-rw-arcs.rs @@ -98,7 +98,7 @@ fn main() { thread_ring(0, msg_per_task, option::unwrap(move num_chan), move num_port); // synchronize - for futures.each |f| { future::get(f) }; + for futures.each |f| { f.get() }; let stop = time::precise_time_s();