about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSteven Fackler <sfackler@gmail.com>2013-09-17 23:48:56 -0700
committerSteven Fackler <sfackler@gmail.com>2013-09-19 15:19:20 -0700
commitff85389344d6fe4a318559b66b97c24b8fddf1e4 (patch)
treeda488f7fe5328471c99eb51e128d6997cf053bc2
parent2df5a13334449c26204333e8b2cdce2dd0cf90eb (diff)
downloadrust-ff85389344d6fe4a318559b66b97c24b8fddf1e4.tar.gz
rust-ff85389344d6fe4a318559b66b97c24b8fddf1e4.zip
Modernize extra::future API
-rw-r--r--doc/tutorial-tasks.md4
-rw-r--r--src/libextra/future.rs132
-rw-r--r--src/libextra/par.rs4
-rw-r--r--src/librustdoc/markdown_writer.rs6
-rw-r--r--src/test/bench/msgsend-ring-mutex-arcs.rs4
-rw-r--r--src/test/bench/msgsend-ring-rw-arcs.rs4
-rw-r--r--src/test/compile-fail/future_not_copyable.rs4
7 files changed, 75 insertions, 83 deletions
diff --git a/doc/tutorial-tasks.md b/doc/tutorial-tasks.md
index aa63a0112d0..e1f70a19e52 100644
--- a/doc/tutorial-tasks.md
+++ b/doc/tutorial-tasks.md
@@ -280,7 +280,7 @@ fn fib(n: uint) -> uint {
     12586269025
 }
 
-let mut delayed_fib = extra::future::spawn (|| fib(50) );
+let mut delayed_fib = extra::future::Future::spawn (|| fib(50) );
 make_a_sandwich();
 println(fmt!("fib(50) = %?", delayed_fib.get()))
 ~~~
@@ -304,7 +304,7 @@ fn partial_sum(start: uint) -> f64 {
 }
 
 fn main() {
-    let mut futures = vec::from_fn(1000, |ind| do extra::future::spawn { partial_sum(ind) });
+    let mut futures = vec::from_fn(1000, |ind| do extra::future::Future::spawn { partial_sum(ind) });
 
     let mut final_res = 0f64;
     for ft in futures.mut_iter()  {
diff --git a/src/libextra/future.rs b/src/libextra/future.rs
index 55e003de9da..2d68cca4adc 100644
--- a/src/libextra/future.rs
+++ b/src/libextra/future.rs
@@ -32,7 +32,7 @@ use std::comm::{PortOne, oneshot};
 use std::task;
 use std::util::replace;
 
-#[doc = "The future type"]
+/// A type encapsulating the result of a computation which may not be complete
 pub struct Future<A> {
     priv state: FutureState<A>,
 }
@@ -62,96 +62,88 @@ impl<A> Future<A> {
             _ => 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
         * pointer to the result.  The borrowed pointer lasts as long as
         * the future.
         */
-        unsafe {
-            {
-                match self.state {
-                    Forced(ref mut v) => { return cast::transmute(v); }
-                    Evaluating => fail!("Recursive forcing of future!"),
-                    Pending(_) => {}
-                }
-            }
-            {
-                let state = replace(&mut self.state, Evaluating);
-                match state {
+        match self.state {
+            Forced(ref v) => return v,
+            Evaluating => fail!("Recursive forcing of future!"),
+            Pending(_) => {
+                match replace(&mut self.state, Evaluating) {
                     Forced(_) | Evaluating => fail!("Logic error."),
                     Pending(f) => {
                         self.state = Forced(f());
-                        cast::transmute(self.get_ref())
+                        self.get_ref()
                     }
                 }
             }
         }
     }
-}
 
-pub fn from_value<A>(val: A) -> Future<A> {
-    /*!
-     * Create a future from a value.
-     *
-     * The value is immediately available and calling `get` later will
-     * not block.
-     */
-
-    Future {state: Forced(val)}
-}
+    pub fn from_value(val: A) -> Future<A> {
+        /*!
+         * Create a future from a value.
+         *
+         * The value is immediately available and calling `get` later will
+         * not block.
+         */
 
-pub fn from_port<A:Send>(port: PortOne<A>) -> Future<A> {
-    /*!
-     * Create a future from a port
-     *
-     * The first time that the value is requested the task will block
-     * waiting for the result to be received on the port.
-     */
+        Future {state: Forced(val)}
+    }
 
-    let port = Cell::new(port);
-    do from_fn {
-        port.take().recv()
+    pub fn from_fn(f: ~fn() -> A) -> Future<A> {
+        /*!
+         * Create a future from a function.
+         *
+         * The first time that the value is requested it will be retrieved by
+         * calling the function.  Note that this function is a local
+         * function. It is not spawned into another task.
+         */
+
+        Future {state: Pending(f)}
     }
 }
 
-pub fn from_fn<A>(f: ~fn() -> A) -> Future<A> {
-    /*!
-     * Create a future from a function.
-     *
-     * The first time that the value is requested it will be retrieved by
-     * calling the function.  Note that this function is a local
-     * function. It is not spawned into another task.
-     */
+impl<A:Send> Future<A> {
+    pub fn from_port(port: PortOne<A>) -> Future<A> {
+        /*!
+         * Create a future from a port
+         *
+         * The first time that the value is requested the task will block
+         * waiting for the result to be received on the port.
+         */
+
+        let port = Cell::new(port);
+        do Future::from_fn {
+            port.take().recv()
+        }
+    }
 
-    Future {state: Pending(f)}
-}
+    pub fn spawn(blk: ~fn() -> A) -> Future<A> {
+        /*!
+         * Create a future from a unique closure.
+         *
+         * The closure will be run in a new task and its result used as the
+         * value of the future.
+         */
 
-pub fn spawn<A:Send>(blk: ~fn() -> A) -> Future<A> {
-    /*!
-     * Create a future from a unique closure.
-     *
-     * The closure will be run in a new task and its result used as the
-     * value of the future.
-     */
+        let (port, chan) = oneshot();
 
-    let (port, chan) = oneshot();
+        do task::spawn_with(chan) |chan| {
+            chan.send(blk());
+        }
 
-    let chan = Cell::new(chan);
-    do task::spawn {
-        let chan = chan.take();
-        chan.send(blk());
+        Future::from_port(port)
     }
-
-    return from_port(port);
 }
 
 #[cfg(test)]
 mod test {
-    use future::*;
+    use future::Future;
 
     use std::cell::Cell;
     use std::comm::oneshot;
@@ -159,7 +151,7 @@ mod test {
 
     #[test]
     fn test_from_value() {
-        let mut f = from_value(~"snail");
+        let mut f = Future::from_value(~"snail");
         assert_eq!(f.get(), ~"snail");
     }
 
@@ -167,51 +159,51 @@ mod test {
     fn test_from_port() {
         let (po, ch) = oneshot();
         ch.send(~"whale");
-        let mut f = from_port(po);
+        let mut f = Future::from_port(po);
         assert_eq!(f.get(), ~"whale");
     }
 
     #[test]
     fn test_from_fn() {
-        let mut f = from_fn(|| ~"brail");
+        let mut f = Future::from_fn(|| ~"brail");
         assert_eq!(f.get(), ~"brail");
     }
 
     #[test]
     fn test_interface_get() {
-        let mut f = from_value(~"fail");
+        let mut f = Future::from_value(~"fail");
         assert_eq!(f.get(), ~"fail");
     }
 
     #[test]
     fn test_interface_unwrap() {
-        let f = from_value(~"fail");
+        let f = Future::from_value(~"fail");
         assert_eq!(f.unwrap(), ~"fail");
     }
 
     #[test]
     fn test_get_ref_method() {
-        let mut f = from_value(22);
+        let mut f = Future::from_value(22);
         assert_eq!(*f.get_ref(), 22);
     }
 
     #[test]
     fn test_spawn() {
-        let mut f = spawn(|| ~"bale");
+        let mut f = Future::spawn(|| ~"bale");
         assert_eq!(f.get(), ~"bale");
     }
 
     #[test]
     #[should_fail]
     fn test_futurefail() {
-        let mut f = spawn(|| fail!());
+        let mut f = Future::spawn(|| fail!());
         let _x: ~str = f.get();
     }
 
     #[test]
     fn test_sendable_future() {
         let expected = "schlorf";
-        let f = Cell::new(do spawn { expected });
+        let f = Cell::new(do Future::spawn { expected });
         do task::spawn {
             let mut f = f.take();
             let actual = f.get();
diff --git a/src/libextra/par.rs b/src/libextra/par.rs
index 71dddc481ae..b5514315226 100644
--- a/src/libextra/par.rs
+++ b/src/libextra/par.rs
@@ -14,7 +14,7 @@ use std::num;
 use std::ptr;
 use std::sys;
 use std::vec;
-use future_spawn = future::spawn;
+use future::Future;
 
 /**
  * The maximum number of tasks this module will spawn for a single
@@ -55,7 +55,7 @@ fn map_slices<A:Clone + Send,B:Clone + Send>(
             do xs.as_imm_buf |p, _len| {
                 let f = f();
                 let base = base;
-                let f = do future_spawn() || {
+                let f = do Future::spawn() || {
                     unsafe {
                         let len = end - base;
                         let slice = (ptr::offset(p, base as int),
diff --git a/src/librustdoc/markdown_writer.rs b/src/librustdoc/markdown_writer.rs
index a1ea5a68c82..fb58e5c2bf0 100644
--- a/src/librustdoc/markdown_writer.rs
+++ b/src/librustdoc/markdown_writer.rs
@@ -20,7 +20,7 @@ use std::result;
 use std::run;
 use std::str;
 use std::task;
-use extra::future;
+use extra::future::Future;
 
 #[deriving(Clone)]
 pub enum WriteInstr {
@@ -207,10 +207,10 @@ pub fn future_writer_factory(
     (writer_factory, markdown_po)
 }
 
-fn future_writer() -> (Writer, future::Future<~str>) {
+fn future_writer() -> (Writer, Future<~str>) {
     let (port, chan) = comm::stream();
     let writer: ~fn(instr: WriteInstr) = |instr| chan.send(instr.clone());
-    let future = do future::from_fn || {
+    let future = do Future::from_fn || {
         let mut res = ~"";
         loop {
             match port.recv() {
diff --git a/src/test/bench/msgsend-ring-mutex-arcs.rs b/src/test/bench/msgsend-ring-mutex-arcs.rs
index dd56d550e61..b52ba154f43 100644
--- a/src/test/bench/msgsend-ring-mutex-arcs.rs
+++ b/src/test/bench/msgsend-ring-mutex-arcs.rs
@@ -18,7 +18,7 @@
 extern mod extra;
 
 use extra::arc;
-use extra::future;
+use extra::future::Future;
 use extra::time;
 use std::cell::Cell;
 use std::os;
@@ -94,7 +94,7 @@ fn main() {
         let (new_chan, num_port) = init();
         let num_chan2 = Cell::new(num_chan.take());
         let num_port = Cell::new(num_port);
-        let new_future = do future::spawn() {
+        let new_future = do Future::spawn() {
             let num_chan = num_chan2.take();
             let num_port1 = num_port.take();
             thread_ring(i, msg_per_task, num_chan, num_port1)
diff --git a/src/test/bench/msgsend-ring-rw-arcs.rs b/src/test/bench/msgsend-ring-rw-arcs.rs
index 130bd4e7d16..e3d0b4912f9 100644
--- a/src/test/bench/msgsend-ring-rw-arcs.rs
+++ b/src/test/bench/msgsend-ring-rw-arcs.rs
@@ -18,7 +18,7 @@
 extern mod extra;
 
 use extra::arc;
-use extra::future;
+use extra::future::Future;
 use extra::time;
 use std::cell::Cell;
 use std::os;
@@ -90,7 +90,7 @@ fn main() {
         let (new_chan, num_port) = init();
         let num_chan2 = Cell::new(num_chan.take());
         let num_port = Cell::new(num_port);
-        let new_future = do future::spawn {
+        let new_future = do Future::spawn {
             let num_chan = num_chan2.take();
             let num_port1 = num_port.take();
             thread_ring(i, msg_per_task, num_chan, num_port1)
diff --git a/src/test/compile-fail/future_not_copyable.rs b/src/test/compile-fail/future_not_copyable.rs
index 7ffa76d4096..aef5d0f9b04 100644
--- a/src/test/compile-fail/future_not_copyable.rs
+++ b/src/test/compile-fail/future_not_copyable.rs
@@ -10,10 +10,10 @@
 
 extern mod extra;
 
-use extra::future;
+use extra::future::Future;
 
 fn main() {
-    let f = future::from_value(());
+    let f = Future::from_value(());
     let g = f;
     f.unwrap(); //~ ERROR use of moved value
 }