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 * use std::sync::Future;
19 * # fn fib(n: uint) -> uint {42};
20 * # fn make_a_sandwich() {};
21 * let mut delayed_fib = Future::spawn(proc() { fib(5000) });
23 * println!("fib(5000) = {}", delayed_fib.get())
27 #![allow(missing_doc)]
30 use core::mem::replace;
32 use comm::{Receiver, channel};
35 /// A type encapsulating the result of a computation which may not be complete
36 pub struct Future<A> {
37 state: FutureState<A>,
41 Pending(proc():Send -> A),
46 /// Methods on the `future` type
47 impl<A:Clone> Future<A> {
48 pub fn get(&mut self) -> A {
49 //! Get the value of the future.
50 (*(self.get_ref())).clone()
55 /// Gets the value from this future, forcing evaluation.
56 pub fn unwrap(mut self) -> A {
58 let state = replace(&mut self.state, Evaluating);
61 _ => fail!( "Logic error." ),
65 pub fn get_ref<'a>(&'a mut self) -> &'a A {
67 * Executes the future's closure and then returns a reference
68 * to the result. The reference lasts as long as
72 Forced(ref v) => return v,
73 Evaluating => fail!("Recursive forcing of future!"),
75 match replace(&mut self.state, Evaluating) {
76 Forced(_) | Evaluating => fail!("Logic error."),
78 self.state = Forced(f());
86 pub fn from_value(val: A) -> Future<A> {
88 * Create a future from a value.
90 * The value is immediately available and calling `get` later will
94 Future {state: Forced(val)}
97 pub fn from_fn(f: proc():Send -> A) -> Future<A> {
99 * Create a future from a function.
101 * The first time that the value is requested it will be retrieved by
102 * calling the function. Note that this function is a local
103 * function. It is not spawned into another task.
106 Future {state: Pending(f)}
110 impl<A:Send> Future<A> {
111 pub fn from_receiver(rx: Receiver<A>) -> Future<A> {
113 * Create a future from a port
115 * The first time that the value is requested the task will block
116 * waiting for the result to be received on the port.
119 Future::from_fn(proc() {
124 pub fn spawn(blk: proc():Send -> A) -> Future<A> {
126 * Create a future from a unique closure.
128 * The closure will be run in a new task and its result used as the
129 * value of the future.
132 let (tx, rx) = channel();
135 // Don't fail if the other end has hung up
136 let _ = tx.send_opt(blk());
139 Future::from_receiver(rx)
148 use comm::{channel, Sender};
151 fn test_from_value() {
152 let mut f = Future::from_value("snail".to_string());
153 assert_eq!(f.get(), "snail".to_string());
157 fn test_from_receiver() {
158 let (tx, rx) = channel();
159 tx.send("whale".to_string());
160 let mut f = Future::from_receiver(rx);
161 assert_eq!(f.get(), "whale".to_string());
166 let mut f = Future::from_fn(proc() "brail".to_string());
167 assert_eq!(f.get(), "brail".to_string());
171 fn test_interface_get() {
172 let mut f = Future::from_value("fail".to_string());
173 assert_eq!(f.get(), "fail".to_string());
177 fn test_interface_unwrap() {
178 let f = Future::from_value("fail".to_string());
179 assert_eq!(f.unwrap(), "fail".to_string());
183 fn test_get_ref_method() {
184 let mut f = Future::from_value(22i);
185 assert_eq!(*f.get_ref(), 22);
190 let mut f = Future::spawn(proc() "bale".to_string());
191 assert_eq!(f.get(), "bale".to_string());
196 fn test_futurefail() {
197 let mut f = Future::spawn(proc() fail!());
198 let _x: String = f.get();
202 fn test_sendable_future() {
203 let expected = "schlorf";
204 let f = Future::spawn(proc() { expected });
207 let actual = f.get();
208 assert_eq!(actual, expected);
213 fn test_dropped_future_doesnt_fail() {
214 struct Bomb(Sender<bool>);
216 local_data_key!(LOCAL: Bomb)
220 let Bomb(ref tx) = *self;
221 tx.send(task::failing());
225 // Spawn a future, but drop it immediately. When we receive the result
226 // later on, we should never view the task as having failed.
227 let (tx, rx) = channel();
228 drop(Future::spawn(proc() {
229 LOCAL.replace(Some(Bomb(tx)));
232 // Make sure the future didn't fail the task.