@@ -15,6 +15,7 @@ export future;
15
15
export future:: { } ;
16
16
export from_value;
17
17
export from_port;
18
+ export from_fn;
18
19
export get;
19
20
export with;
20
21
export spawn;
@@ -23,7 +24,7 @@ import either = either::t;
23
24
24
25
#[ doc = "The future type" ]
25
26
enum future < A > = {
26
- mutable v: either<@A , comm :: port< A > >
27
+ mutable v: either<@A , fn@ ( ) -> A >
27
28
} ;
28
29
29
30
#[ doc = "Methods on the `future` type" ]
@@ -55,7 +56,7 @@ fn from_value<A>(+val: A) -> future<A> {
55
56
} )
56
57
}
57
58
58
- fn from_port < A > ( -port : comm:: port < A > ) -> future < A > {
59
+ fn from_port < A : send > ( -port : comm:: port < A > ) -> future < A > {
59
60
#[ doc = "
60
61
61
62
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> {
64
65
65
66
" ] ;
66
67
67
- future ( {
68
- mutable v : either :: right ( port)
69
- } )
68
+ from_fn { ||
69
+ comm :: recv ( port)
70
+ }
70
71
}
71
72
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 = "
74
75
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.
77
78
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
+ " ] ;
80
82
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
+ } )
90
86
}
91
87
92
88
fn spawn < A : send > ( +blk : fn ~( ) -> A ) -> future < A > {
@@ -105,6 +101,26 @@ fn spawn<A:send>(+blk: fn~() -> A) -> future<A> {
105
101
from_port ( po)
106
102
}
107
103
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
+
108
124
#[ test]
109
125
fn test_from_value ( ) {
110
126
let f = from_value ( "snail" ) ;
@@ -120,6 +136,13 @@ fn test_from_port() {
120
136
assert get( f) == "whale" ;
121
137
}
122
138
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
+
123
146
#[ test]
124
147
fn test_iface_get ( ) {
125
148
let f = from_value ( "fail" ) ;
0 commit comments