1 // Copyright 2012 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 * let delayed_fib = future::spawn {|| fib(5000) };
20 * io::println(fmt!("fib(5000) = %?", delayed_fib.get()))
26 use core
::comm
::{oneshot, PortOne, send_one}
;
27 use core
::pipes
::recv
;
31 #[doc = "The future type"]
32 pub struct Future
<A
> {
33 priv mut state
: FutureState
<A
>,
36 // FIXME(#2829) -- futures should not be copyable, because they close
37 // over ~fn's that have pipes and so forth within!
39 impl<A
> Drop
for Future
<A
> {
43 priv enum FutureState
<A
> {
49 /// Methods on the `future` type
50 pub impl<A
:Copy
> Future
<A
> {
52 //! Get the value of the future
57 pub impl<A
> Future
<A
> {
59 fn get_ref(&self) -> &'
self A
{
61 * Executes the future's closure and then returns a borrowed
62 * pointer to the result. The borrowed pointer lasts as long as
67 Forced(ref mut v
) => { return cast::transmute(v); }
68 Evaluating
=> fail
!(~"Recursive forcing of future!"),
72 let mut state
= Evaluating
;
75 Forced(_
) | Evaluating
=> fail
!(~"Logic error."),
77 self.state
= Forced(f());
85 pub fn from_value
<A
>(val
: A
) -> Future
<A
> {
87 * Create a future from a value
89 * The value is immediately available and calling `get` later will
93 Future {state: Forced(val)}
96 pub fn from_port
<A
:Owned
>(port
: PortOne
<A
>) ->
99 * Create a future from a port
101 * The first time that the value is requested the task will block
102 * waiting for the result to be received on the port.
105 let port
= Cell(port
);
107 let port
= port
.take();
109 oneshot
::send(data
) => data
114 pub fn from_fn
<A
>(f
: ~fn() -> A
) -> Future
<A
> {
116 * Create a future from a function.
118 * The first time that the value is requested it will be retreived by
119 * calling the function. Note that this function is a local
120 * function. It is not spawned into another task.
123 Future {state: Pending(f)}
126 pub fn spawn
<A
:Owned
>(blk
: ~fn() -> A
) -> Future
<A
> {
128 * Create a future from a unique closure.
130 * The closure will be run in a new task and its result used as the
131 * value of the future.
134 let (chan
, port
) = oneshot
::init();
136 let chan
= Cell(chan
);
138 let chan
= chan
.take();
139 send_one(chan
, blk());
142 return from_port(port
);
145 #[allow(non_implicitly_copyable_typarams)]
148 use core
::prelude
::*;
152 use core
::comm
::{oneshot, send_one}
;
156 pub fn test_from_value() {
157 let f
= from_value(~"snail");
158 assert
!(f
.get() == ~"snail");
162 pub fn test_from_port() {
163 let (ch
, po
) = oneshot
::init();
164 send_one(ch
, ~"whale");
165 let f
= from_port(po
);
166 assert
!(f
.get() == ~"whale");
170 pub fn test_from_fn() {
171 let f
= from_fn(|| ~"brail");
172 assert
!(f
.get() == ~"brail");
176 pub fn test_interface_get() {
177 let f
= from_value(~"fail");
178 assert
!(f
.get() == ~"fail");
182 pub fn test_get_ref_method() {
183 let f
= from_value(22);
184 assert
!(*f
.get_ref() == 22);
188 pub fn test_spawn() {
189 let f
= spawn(|| ~"bale");
190 assert
!(f
.get() == ~"bale");
195 #[ignore(cfg(target_os = "win32"))]
196 pub fn test_futurefail() {
197 let f
= spawn(|| fail
!());
198 let _x
: ~str = f
.get();
202 pub fn test_sendable_future() {
203 let expected
= ~"schlorf";
204 let f
= do spawn { copy expected }
;
206 let actual
= f
.get();
207 assert
!(actual
== expected
);