Skip to content

Commit 23d36be

Browse files
committed
core: Define futures in terms of local functions, of which port::recv is one possibility
1 parent d2294a2 commit 23d36be

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

src/libcore/future.rs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export future;
1515
export future::{};
1616
export from_value;
1717
export from_port;
18+
export from_fn;
1819
export get;
1920
export with;
2021
export spawn;
@@ -23,7 +24,7 @@ import either = either::t;
2324

2425
#[doc = "The future type"]
2526
enum future<A> = {
26-
mutable v: either<@A, comm::port<A>>
27+
mutable v: either<@A, fn@() -> A>
2728
};
2829

2930
#[doc = "Methods on the `future` type"]
@@ -55,7 +56,7 @@ fn from_value<A>(+val: A) -> future<A> {
5556
})
5657
}
5758

58-
fn from_port<A>(-port: comm::port<A>) -> future<A> {
59+
fn from_port<A:send>(-port: comm::port<A>) -> future<A> {
5960
#[doc = "
6061
6162
Create a future from a port. The first time that the value is
@@ -64,29 +65,24 @@ fn from_port<A>(-port: comm::port<A>) -> future<A> {
6465
6566
"];
6667

67-
future({
68-
mutable v: either::right(port)
69-
})
68+
from_fn {||
69+
comm::recv(port)
70+
}
7071
}
7172

72-
fn get<A:send>(future: future<A>) -> A {
73-
#[doc = "Get the value of the future"];
73+
fn from_fn<A>(f: fn@() -> A) -> future<A> {
74+
#[doc = "
7475
75-
with(future) {|v| v }
76-
}
76+
Create a future from a function. The first time that the value is
77+
requested it will be retreived by calling the function.
7778
78-
fn with<A:send,B>(future: future<A>, blk: fn(A) -> B) -> B {
79-
#[doc = "Work with the value without copying it"];
79+
Note that this function is a local function. It is not spawned into
80+
another task.
81+
"];
8082

81-
let v = alt future.v {
82-
either::left(v) { v }
83-
either::right(po) {
84-
let v = @comm::recv(po);
85-
future.v = either::left(v);
86-
v
87-
}
88-
};
89-
blk(*v)
83+
future({
84+
mutable v: either::right(f)
85+
})
9086
}
9187

9288
fn spawn<A:send>(+blk: fn~() -> A) -> future<A> {
@@ -105,6 +101,26 @@ fn spawn<A:send>(+blk: fn~() -> A) -> future<A> {
105101
from_port(po)
106102
}
107103

104+
fn get<A:copy>(future: future<A>) -> A {
105+
#[doc = "Get the value of the future"];
106+
107+
with(future) {|v| v }
108+
}
109+
110+
fn with<A,B>(future: future<A>, blk: fn(A) -> B) -> B {
111+
#[doc = "Work with the value without copying it"];
112+
113+
let v = alt future.v {
114+
either::left(v) { v }
115+
either::right(f) {
116+
let v = @f();
117+
future.v = either::left(v);
118+
v
119+
}
120+
};
121+
blk(*v)
122+
}
123+
108124
#[test]
109125
fn test_from_value() {
110126
let f = from_value("snail");
@@ -120,6 +136,13 @@ fn test_from_port() {
120136
assert get(f) == "whale";
121137
}
122138

139+
#[test]
140+
fn test_from_fn() {
141+
let f = fn@() -> str { "brail" };
142+
let f = from_fn(f);
143+
assert get(f) == "brail";
144+
}
145+
123146
#[test]
124147
fn test_iface_get() {
125148
let f = from_value("fail");

0 commit comments

Comments
 (0)