Skip to content

Commit 2f637c9

Browse files
committed
---
yaml --- r: 83272 b: refs/heads/try c: c767643 h: refs/heads/master v: v3
1 parent 6b6e90e commit 2f637c9

File tree

12 files changed

+123
-132
lines changed

12 files changed

+123
-132
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 0e4d1fc8cae42e15e00f71d9f439b01bb25a86ae
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 6c08cc2db4f98e9f07ae7d50338396c4123c2f0a
5-
refs/heads/try: 407d179f4e0b625e6911ebaf72c87cd35935fb06
5+
refs/heads/try: c767643f2b7f3b47b98acf424ceaf6826c0475bc
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/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::Future::spawn (|| fib(50) );
283+
let mut delayed_fib = extra::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::Future::spawn { partial_sum(ind) });
307+
let mut futures = vec::from_fn(1000, |ind| do extra::future::spawn { partial_sum(ind) });
308308
309309
let mut final_res = 0f64;
310310
for ft in futures.mut_iter() {

branches/try/src/libextra/fileinput.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ mod test {
420420
use std::rt::io;
421421
use std::rt::io::Writer;
422422
use std::rt::io::file;
423+
use std::uint;
423424
use std::vec;
424425

425426
fn make_file(path : &Path, contents: &[~str]) {

branches/try/src/libextra/future.rs

Lines changed: 80 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,27 @@
2525

2626
#[allow(missing_doc)];
2727

28+
29+
use std::cast;
2830
use std::cell::Cell;
2931
use std::comm::{PortOne, oneshot};
3032
use std::task;
3133
use std::util::replace;
3234

33-
/// A type encapsulating the result of a computation which may not be complete
35+
#[doc = "The future type"]
3436
pub struct Future<A> {
3537
priv state: FutureState<A>,
3638
}
3739

40+
// n.b. It should be possible to get rid of this.
41+
// Add a test, though -- tjc
42+
// FIXME(#2829) -- futures should not be copyable, because they close
43+
// over ~fn's that have pipes and so forth within!
44+
#[unsafe_destructor]
45+
impl<A> Drop for Future<A> {
46+
fn drop(&mut self) {}
47+
}
48+
3849
enum FutureState<A> {
3950
Pending(~fn() -> A),
4051
Evaluating,
@@ -60,171 +71,156 @@ impl<A> Future<A> {
6071
_ => fail!( "Logic error." ),
6172
}
6273
}
74+
}
6375

76+
impl<A> Future<A> {
6477
pub fn get_ref<'a>(&'a mut self) -> &'a A {
6578
/*!
6679
* Executes the future's closure and then returns a borrowed
6780
* pointer to the result. The borrowed pointer lasts as long as
6881
* the future.
6982
*/
70-
match self.state {
71-
Forced(ref v) => return v,
72-
Evaluating => fail!("Recursive forcing of future!"),
73-
Pending(_) => {
74-
match replace(&mut self.state, Evaluating) {
83+
unsafe {
84+
{
85+
match self.state {
86+
Forced(ref mut v) => { return cast::transmute(v); }
87+
Evaluating => fail!("Recursive forcing of future!"),
88+
Pending(_) => {}
89+
}
90+
}
91+
{
92+
let state = replace(&mut self.state, Evaluating);
93+
match state {
7594
Forced(_) | Evaluating => fail!("Logic error."),
7695
Pending(f) => {
7796
self.state = Forced(f());
78-
self.get_ref()
97+
cast::transmute(self.get_ref())
7998
}
8099
}
81100
}
82101
}
83102
}
103+
}
84104

85-
pub fn from_value(val: A) -> Future<A> {
86-
/*!
87-
* Create a future from a value.
88-
*
89-
* The value is immediately available and calling `get` later will
90-
* not block.
91-
*/
105+
pub fn from_value<A>(val: A) -> Future<A> {
106+
/*!
107+
* Create a future from a value.
108+
*
109+
* The value is immediately available and calling `get` later will
110+
* not block.
111+
*/
92112

93-
Future {state: Forced(val)}
94-
}
113+
Future {state: Forced(val)}
114+
}
95115

96-
pub fn from_fn(f: ~fn() -> A) -> Future<A> {
97-
/*!
98-
* Create a future from a function.
99-
*
100-
* The first time that the value is requested it will be retrieved by
101-
* calling the function. Note that this function is a local
102-
* function. It is not spawned into another task.
103-
*/
104-
105-
Future {state: Pending(f)}
116+
pub fn from_port<A:Send>(port: PortOne<A>) -> Future<A> {
117+
/*!
118+
* Create a future from a port
119+
*
120+
* The first time that the value is requested the task will block
121+
* waiting for the result to be received on the port.
122+
*/
123+
124+
let port = Cell::new(port);
125+
do from_fn {
126+
port.take().recv()
106127
}
107128
}
108129

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

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

132-
let (port, chan) = oneshot();
142+
pub fn spawn<A:Send>(blk: ~fn() -> A) -> Future<A> {
143+
/*!
144+
* Create a future from a unique closure.
145+
*
146+
* The closure will be run in a new task and its result used as the
147+
* value of the future.
148+
*/
133149

134-
do task::spawn_with(chan) |chan| {
135-
chan.send(blk());
136-
}
150+
let (port, chan) = oneshot();
137151

138-
Future::from_port(port)
152+
let chan = Cell::new(chan);
153+
do task::spawn {
154+
let chan = chan.take();
155+
chan.send(blk());
139156
}
140157

141-
pub fn spawn_with<B: Send>(v: B, blk: ~fn(B) -> A) -> Future<A> {
142-
/*!
143-
* Create a future from a unique closure taking one argument.
144-
*
145-
* The closure and its argument will be moved into a new task. The
146-
* closure will be run and its result used as the value of the future.
147-
*/
148-
149-
let (port, chan) = oneshot();
150-
151-
do task::spawn_with((v, chan)) |(v, chan)| {
152-
chan.send(blk(v));
153-
}
154-
155-
Future::from_port(port)
156-
}
158+
return from_port(port);
157159
}
158160

159161
#[cfg(test)]
160162
mod test {
161-
use future::Future;
163+
use future::*;
162164

163165
use std::cell::Cell;
164166
use std::comm::oneshot;
165167
use std::task;
166168

167169
#[test]
168170
fn test_from_value() {
169-
let mut f = Future::from_value(~"snail");
171+
let mut f = from_value(~"snail");
170172
assert_eq!(f.get(), ~"snail");
171173
}
172174
173175
#[test]
174176
fn test_from_port() {
175177
let (po, ch) = oneshot();
176178
ch.send(~"whale");
177-
let mut f = Future::from_port(po);
179+
let mut f = from_port(po);
178180
assert_eq!(f.get(), ~"whale");
179181
}
180182
181183
#[test]
182184
fn test_from_fn() {
183-
let mut f = Future::from_fn(|| ~"brail");
185+
let mut f = from_fn(|| ~"brail");
184186
assert_eq!(f.get(), ~"brail");
185187
}
186188
187189
#[test]
188190
fn test_interface_get() {
189-
let mut f = Future::from_value(~"fail");
191+
let mut f = from_value(~"fail");
190192
assert_eq!(f.get(), ~"fail");
191193
}
192194
193195
#[test]
194196
fn test_interface_unwrap() {
195-
let f = Future::from_value(~"fail");
197+
let f = from_value(~"fail");
196198
assert_eq!(f.unwrap(), ~"fail");
197199
}
198200
199201
#[test]
200202
fn test_get_ref_method() {
201-
let mut f = Future::from_value(22);
203+
let mut f = from_value(22);
202204
assert_eq!(*f.get_ref(), 22);
203205
}
204206
205207
#[test]
206208
fn test_spawn() {
207-
let mut f = Future::spawn(|| ~"bale");
209+
let mut f = spawn(|| ~"bale");
208210
assert_eq!(f.get(), ~"bale");
209211
}
210212
211-
#[test]
212-
fn test_spawn_with() {
213-
let mut f = Future::spawn_with(~"gale", |s| { s });
214-
assert_eq!(f.get(), ~"gale");
215-
}
216-
217213
#[test]
218214
#[should_fail]
219215
fn test_futurefail() {
220-
let mut f = Future::spawn(|| fail!());
216+
let mut f = spawn(|| fail!());
221217
let _x: ~str = f.get();
222218
}
223219
224220
#[test]
225221
fn test_sendable_future() {
226222
let expected = "schlorf";
227-
let f = Cell::new(do Future::spawn { expected });
223+
let f = Cell::new(do spawn { expected });
228224
do task::spawn {
229225
let mut f = f.take();
230226
let actual = f.get();

branches/try/src/libextra/glob.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,10 @@ impl MatchOptions {
512512
513513
#[cfg(test)]
514514
mod test {
515-
use std::os;
515+
use std::{io, os, unstable};
516+
use std::unstable::finally::Finally;
516517
use super::*;
518+
use tempfile;
517519
518520
#[test]
519521
fn test_absolute_pattern() {

branches/try/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::Future;
17+
use future_spawn = future::spawn;
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),

branches/try/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::Future;
23+
use extra::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<~str>) {
210+
fn future_writer() -> (Writer, future::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() {

0 commit comments

Comments
 (0)