Skip to content

Commit ff85389

Browse files
committed
Modernize extra::future API
1 parent 2df5a13 commit ff85389

File tree

7 files changed

+75
-83
lines changed

7 files changed

+75
-83
lines changed

doc/tutorial-tasks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn fib(n: uint) -> uint {
280280
12586269025
281281
}
282282
283-
let mut delayed_fib = extra::future::spawn (|| fib(50) );
283+
let mut delayed_fib = extra::future::Future::spawn (|| fib(50) );
284284
make_a_sandwich();
285285
println(fmt!("fib(50) = %?", delayed_fib.get()))
286286
~~~
@@ -304,7 +304,7 @@ fn partial_sum(start: uint) -> f64 {
304304
}
305305
306306
fn main() {
307-
let mut futures = vec::from_fn(1000, |ind| do extra::future::spawn { partial_sum(ind) });
307+
let mut futures = vec::from_fn(1000, |ind| do extra::future::Future::spawn { partial_sum(ind) });
308308
309309
let mut final_res = 0f64;
310310
for ft in futures.mut_iter() {

src/libextra/future.rs

Lines changed: 62 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::comm::{PortOne, oneshot};
3232
use std::task;
3333
use std::util::replace;
3434

35-
#[doc = "The future type"]
35+
/// A type encapsulating the result of a computation which may not be complete
3636
pub struct Future<A> {
3737
priv state: FutureState<A>,
3838
}
@@ -62,156 +62,148 @@ impl<A> Future<A> {
6262
_ => fail!( "Logic error." ),
6363
}
6464
}
65-
}
6665

67-
impl<A> Future<A> {
6866
pub fn get_ref<'a>(&'a mut self) -> &'a A {
6967
/*!
7068
* Executes the future's closure and then returns a borrowed
7169
* pointer to the result. The borrowed pointer lasts as long as
7270
* the future.
7371
*/
74-
unsafe {
75-
{
76-
match self.state {
77-
Forced(ref mut v) => { return cast::transmute(v); }
78-
Evaluating => fail!("Recursive forcing of future!"),
79-
Pending(_) => {}
80-
}
81-
}
82-
{
83-
let state = replace(&mut self.state, Evaluating);
84-
match state {
72+
match self.state {
73+
Forced(ref v) => return v,
74+
Evaluating => fail!("Recursive forcing of future!"),
75+
Pending(_) => {
76+
match replace(&mut self.state, Evaluating) {
8577
Forced(_) | Evaluating => fail!("Logic error."),
8678
Pending(f) => {
8779
self.state = Forced(f());
88-
cast::transmute(self.get_ref())
80+
self.get_ref()
8981
}
9082
}
9183
}
9284
}
9385
}
94-
}
9586

96-
pub fn from_value<A>(val: A) -> Future<A> {
97-
/*!
98-
* Create a future from a value.
99-
*
100-
* The value is immediately available and calling `get` later will
101-
* not block.
102-
*/
103-
104-
Future {state: Forced(val)}
105-
}
87+
pub fn from_value(val: A) -> Future<A> {
88+
/*!
89+
* Create a future from a value.
90+
*
91+
* The value is immediately available and calling `get` later will
92+
* not block.
93+
*/
10694

107-
pub fn from_port<A:Send>(port: PortOne<A>) -> Future<A> {
108-
/*!
109-
* Create a future from a port
110-
*
111-
* The first time that the value is requested the task will block
112-
* waiting for the result to be received on the port.
113-
*/
95+
Future {state: Forced(val)}
96+
}
11497

115-
let port = Cell::new(port);
116-
do from_fn {
117-
port.take().recv()
98+
pub fn from_fn(f: ~fn() -> A) -> Future<A> {
99+
/*!
100+
* Create a future from a function.
101+
*
102+
* The first time that the value is requested it will be retrieved by
103+
* calling the function. Note that this function is a local
104+
* function. It is not spawned into another task.
105+
*/
106+
107+
Future {state: Pending(f)}
118108
}
119109
}
120110

121-
pub fn from_fn<A>(f: ~fn() -> A) -> Future<A> {
122-
/*!
123-
* Create a future from a function.
124-
*
125-
* The first time that the value is requested it will be retrieved by
126-
* calling the function. Note that this function is a local
127-
* function. It is not spawned into another task.
128-
*/
111+
impl<A:Send> Future<A> {
112+
pub fn from_port(port: PortOne<A>) -> Future<A> {
113+
/*!
114+
* Create a future from a port
115+
*
116+
* The first time that the value is requested the task will block
117+
* waiting for the result to be received on the port.
118+
*/
119+
120+
let port = Cell::new(port);
121+
do Future::from_fn {
122+
port.take().recv()
123+
}
124+
}
129125

130-
Future {state: Pending(f)}
131-
}
126+
pub fn spawn(blk: ~fn() -> A) -> Future<A> {
127+
/*!
128+
* Create a future from a unique closure.
129+
*
130+
* The closure will be run in a new task and its result used as the
131+
* value of the future.
132+
*/
132133

133-
pub fn spawn<A:Send>(blk: ~fn() -> A) -> Future<A> {
134-
/*!
135-
* Create a future from a unique closure.
136-
*
137-
* The closure will be run in a new task and its result used as the
138-
* value of the future.
139-
*/
134+
let (port, chan) = oneshot();
140135

141-
let (port, chan) = oneshot();
136+
do task::spawn_with(chan) |chan| {
137+
chan.send(blk());
138+
}
142139

143-
let chan = Cell::new(chan);
144-
do task::spawn {
145-
let chan = chan.take();
146-
chan.send(blk());
140+
Future::from_port(port)
147141
}
148-
149-
return from_port(port);
150142
}
151143

152144
#[cfg(test)]
153145
mod test {
154-
use future::*;
146+
use future::Future;
155147

156148
use std::cell::Cell;
157149
use std::comm::oneshot;
158150
use std::task;
159151

160152
#[test]
161153
fn test_from_value() {
162-
let mut f = from_value(~"snail");
154+
let mut f = Future::from_value(~"snail");
163155
assert_eq!(f.get(), ~"snail");
164156
}
165157
166158
#[test]
167159
fn test_from_port() {
168160
let (po, ch) = oneshot();
169161
ch.send(~"whale");
170-
let mut f = from_port(po);
162+
let mut f = Future::from_port(po);
171163
assert_eq!(f.get(), ~"whale");
172164
}
173165
174166
#[test]
175167
fn test_from_fn() {
176-
let mut f = from_fn(|| ~"brail");
168+
let mut f = Future::from_fn(|| ~"brail");
177169
assert_eq!(f.get(), ~"brail");
178170
}
179171
180172
#[test]
181173
fn test_interface_get() {
182-
let mut f = from_value(~"fail");
174+
let mut f = Future::from_value(~"fail");
183175
assert_eq!(f.get(), ~"fail");
184176
}
185177
186178
#[test]
187179
fn test_interface_unwrap() {
188-
let f = from_value(~"fail");
180+
let f = Future::from_value(~"fail");
189181
assert_eq!(f.unwrap(), ~"fail");
190182
}
191183
192184
#[test]
193185
fn test_get_ref_method() {
194-
let mut f = from_value(22);
186+
let mut f = Future::from_value(22);
195187
assert_eq!(*f.get_ref(), 22);
196188
}
197189
198190
#[test]
199191
fn test_spawn() {
200-
let mut f = spawn(|| ~"bale");
192+
let mut f = Future::spawn(|| ~"bale");
201193
assert_eq!(f.get(), ~"bale");
202194
}
203195
204196
#[test]
205197
#[should_fail]
206198
fn test_futurefail() {
207-
let mut f = spawn(|| fail!());
199+
let mut f = Future::spawn(|| fail!());
208200
let _x: ~str = f.get();
209201
}
210202
211203
#[test]
212204
fn test_sendable_future() {
213205
let expected = "schlorf";
214-
let f = Cell::new(do spawn { expected });
206+
let f = Cell::new(do Future::spawn { expected });
215207
do task::spawn {
216208
let mut f = f.take();
217209
let actual = f.get();

src/libextra/par.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::num;
1414
use std::ptr;
1515
use std::sys;
1616
use std::vec;
17-
use future_spawn = future::spawn;
17+
use future::Future;
1818

1919
/**
2020
* 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>(
5555
do xs.as_imm_buf |p, _len| {
5656
let f = f();
5757
let base = base;
58-
let f = do future_spawn() || {
58+
let f = do Future::spawn() || {
5959
unsafe {
6060
let len = end - base;
6161
let slice = (ptr::offset(p, base as int),

src/librustdoc/markdown_writer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::result;
2020
use std::run;
2121
use std::str;
2222
use std::task;
23-
use extra::future;
23+
use extra::future::Future;
2424

2525
#[deriving(Clone)]
2626
pub enum WriteInstr {
@@ -207,10 +207,10 @@ pub fn future_writer_factory(
207207
(writer_factory, markdown_po)
208208
}
209209

210-
fn future_writer() -> (Writer, future::Future<~str>) {
210+
fn future_writer() -> (Writer, Future<~str>) {
211211
let (port, chan) = comm::stream();
212212
let writer: ~fn(instr: WriteInstr) = |instr| chan.send(instr.clone());
213-
let future = do future::from_fn || {
213+
let future = do Future::from_fn || {
214214
let mut res = ~"";
215215
loop {
216216
match port.recv() {

src/test/bench/msgsend-ring-mutex-arcs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
extern mod extra;
1919

2020
use extra::arc;
21-
use extra::future;
21+
use extra::future::Future;
2222
use extra::time;
2323
use std::cell::Cell;
2424
use std::os;
@@ -94,7 +94,7 @@ fn main() {
9494
let (new_chan, num_port) = init();
9595
let num_chan2 = Cell::new(num_chan.take());
9696
let num_port = Cell::new(num_port);
97-
let new_future = do future::spawn() {
97+
let new_future = do Future::spawn() {
9898
let num_chan = num_chan2.take();
9999
let num_port1 = num_port.take();
100100
thread_ring(i, msg_per_task, num_chan, num_port1)

src/test/bench/msgsend-ring-rw-arcs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
extern mod extra;
1919

2020
use extra::arc;
21-
use extra::future;
21+
use extra::future::Future;
2222
use extra::time;
2323
use std::cell::Cell;
2424
use std::os;
@@ -90,7 +90,7 @@ fn main() {
9090
let (new_chan, num_port) = init();
9191
let num_chan2 = Cell::new(num_chan.take());
9292
let num_port = Cell::new(num_port);
93-
let new_future = do future::spawn {
93+
let new_future = do Future::spawn {
9494
let num_chan = num_chan2.take();
9595
let num_port1 = num_port.take();
9696
thread_ring(i, msg_per_task, num_chan, num_port1)

src/test/compile-fail/future_not_copyable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
extern mod extra;
1212

13-
use extra::future;
13+
use extra::future::Future;
1414

1515
fn main() {
16-
let f = future::from_value(());
16+
let f = Future::from_value(());
1717
let g = f;
1818
f.unwrap(); //~ ERROR use of moved value
1919
}

0 commit comments

Comments
 (0)