1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 * A type representing values that may be computed concurrently and
13 * operations for working with them.
18 * # fn fib(n: uint) -> uint {42};
19 * # fn make_a_sandwich() {};
20 * let mut delayed_fib = std::future::spawn (|| fib(5000) );
22 * println(fmt!("fib(5000) = %?", delayed_fib.get()))
26 #[allow(missing_doc)];
32 use core::comm::{PortOne, oneshot, send_one, recv_one};
34 use core::util::replace;
36 #[doc = "The future type"]
37 pub struct Future<A> {
38 priv state: FutureState<A>,
41 // n.b. It should be possible to get rid of this.
42 // Add a test, though -- tjc
43 // FIXME(#2829) -- futures should not be copyable, because they close
44 // over ~fn's that have pipes and so forth within!
46 impl<A> Drop for Future<A> {
50 priv enum FutureState<A> {
56 /// Methods on the `future` type
57 impl<A:Copy> Future<A> {
58 pub fn get(&mut self) -> A {
59 //! Get the value of the future.
60 copy *(self.get_ref())
65 pub fn get_ref<'a>(&'a mut self) -> &'a A {
67 * Executes the future's closure and then returns a borrowed
68 * pointer to the result. The borrowed pointer lasts as long as
74 Forced(ref mut v) => { return cast::transmute(v); }
75 Evaluating => fail!("Recursive forcing of future!"),
80 let state = replace(&mut self.state, Evaluating);
82 Forced(_) | Evaluating => fail!("Logic error."),
84 self.state = Forced(f());
85 cast::transmute(self.get_ref())
93 pub fn from_value<A>(val: A) -> Future<A> {
95 * Create a future from a value.
97 * The value is immediately available and calling `get` later will
101 Future {state: Forced(val)}
104 pub fn from_port<A:Owned>(port: PortOne<A>) -> Future<A> {
106 * Create a future from a port
108 * The first time that the value is requested the task will block
109 * waiting for the result to be received on the port.
112 let port = Cell::new(port);
114 recv_one(port.take())
118 pub fn from_fn<A>(f: ~fn() -> A) -> Future<A> {
120 * Create a future from a function.
122 * The first time that the value is requested it will be retrieved by
123 * calling the function. Note that this function is a local
124 * function. It is not spawned into another task.
127 Future {state: Pending(f)}
130 pub fn spawn<A:Owned>(blk: ~fn() -> A) -> Future<A> {
132 * Create a future from a unique closure.
134 * The closure will be run in a new task and its result used as the
135 * value of the future.
138 let (port, chan) = oneshot();
140 let chan = Cell::new(chan);
142 let chan = chan.take();
143 send_one(chan, blk());
146 return from_port(port);
149 #[allow(non_implicitly_copyable_typarams)]
154 use core::cell::Cell;
155 use core::comm::{oneshot, send_one};
159 fn test_from_value() {
160 let mut f = from_value(~"snail");
161 assert_eq!(f.get(), ~"snail");
165 fn test_from_port() {
166 let (po, ch) = oneshot();
167 send_one(ch, ~"whale");
168 let mut f = from_port(po);
169 assert_eq!(f.get(), ~"whale");
174 let mut f = from_fn(|| ~"brail");
175 assert_eq!(f.get(), ~"brail");
179 fn test_interface_get() {
180 let mut f = from_value(~"fail");
181 assert_eq!(f.get(), ~"fail");
185 fn test_get_ref_method() {
186 let mut f = from_value(22);
187 assert_eq!(*f.get_ref(), 22);
192 let mut f = spawn(|| ~"bale");
193 assert_eq!(f.get(), ~"bale");
198 #[ignore(cfg(target_os = "win32"))]
199 fn test_futurefail() {
200 let mut f = spawn(|| fail!());
201 let _x: ~str = f.get();
205 fn test_sendable_future() {
206 let expected = "schlorf";
207 let f = Cell::new(do spawn { expected });
209 let mut f = f.take();
210 let actual = f.get();
211 assert_eq!(actual, expected);