8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- // NB: transitionary, de-mode-ing.
12
- // tjc: allowing deprecated modes due to function issue.
13
- // can re-forbid them after snapshot
14
- #[ forbid( deprecated_pattern) ] ;
15
-
16
11
/*!
17
12
* A type representing values that may be computed concurrently and
18
13
* operations for working with them.
@@ -44,27 +39,42 @@ impl<A> Future<A> : Drop {
44
39
priv enum FutureState < A > {
45
40
Pending ( fn ~( ) -> A ) ,
46
41
Evaluating ,
47
- Forced ( ~ A )
42
+ Forced ( A )
48
43
}
49
44
50
45
/// Methods on the `future` type
51
46
impl < A : Copy > Future < A > {
52
47
fn get ( ) -> A {
53
48
//! Get the value of the future
54
-
55
- get ( & self )
49
+ * ( self . get_ref ( ) )
56
50
}
57
51
}
58
52
59
53
impl < A > Future < A > {
60
- fn get_ref ( & self ) -> & self /A {
61
- get_ref ( self )
62
- }
63
-
64
- fn with < B > ( blk : fn ( & A ) -> B ) -> B {
65
- //! Work with the value without copying it
66
54
67
- with ( & self , blk)
55
+ pure fn get_ref ( & self ) -> & self /A {
56
+ /*!
57
+ * Executes the future's closure and then returns a borrowed
58
+ * pointer to the result. The borrowed pointer lasts as long as
59
+ * the future.
60
+ */
61
+ unsafe {
62
+ match self . state {
63
+ Forced ( ref mut v) => { return cast:: transmute ( v) ; }
64
+ Evaluating => fail ~"Recursive forcing of future!",
65
+ Pending(_) => {}
66
+ }
67
+
68
+ let mut state = Evaluating;
69
+ self.state <-> state;
70
+ match move state {
71
+ Forced(_) | Evaluating => fail ~" Logic error. ",
72
+ Pending ( move f) => {
73
+ self . state = Forced ( move f ( ) ) ;
74
+ self . get_ref ( )
75
+ }
76
+ }
77
+ }
68
78
}
69
79
}
70
80
@@ -76,7 +86,7 @@ pub fn from_value<A>(val: A) -> Future<A> {
76
86
* not block.
77
87
*/
78
88
79
- Future { state : Forced ( ~ ( move val) ) }
89
+ Future { state : Forced ( move val) }
80
90
}
81
91
82
92
pub fn from_port < A : Send > ( port : PortOne < A > ) ->
@@ -130,76 +140,26 @@ pub fn spawn<A:Send>(blk: fn~() -> A) -> Future<A> {
130
140
return from_port ( move port) ;
131
141
}
132
142
133
- pub fn get_ref < A > ( future : & r/Future < A > ) -> & r /A {
134
- /*!
135
- * Executes the future's closure and then returns a borrowed
136
- * pointer to the result. The borrowed pointer lasts as long as
137
- * the future.
138
- */
139
-
140
- // The unsafety here is to hide the aliases from borrowck, which
141
- // would otherwise be concerned that someone might reassign
142
- // `future.state` and cause the value of the future to be freed.
143
- // But *we* know that once `future.state` is `Forced()` it will
144
- // never become "unforced"---so we can safely return a pointer
145
- // into the interior of the Forced() variant which will last as
146
- // long as the future itself.
147
-
148
- match future. state {
149
- Forced ( ref v) => { // v here has type &A, but with a shorter lifetime.
150
- return unsafe { copy_lifetime ( future, & * * v) } ; // ...extend it.
151
- }
152
- Evaluating => {
153
- fail ~"Recursive forcing of future!";
154
- }
155
- Pending(_) => {}
156
- }
157
-
158
- let mut state = Evaluating;
159
- state <-> future.state;
160
- match move state {
161
- Forced(_) | Evaluating => {
162
- fail ~" Logic error. ";
163
- }
164
- Pending ( move f) => {
165
- future. state = Forced ( ~f ( ) ) ;
166
- return get_ref ( future) ;
167
- }
168
- }
169
- }
170
-
171
- pub fn get < A : Copy > ( future : & Future < A > ) -> A {
172
- //! Get the value of the future
173
-
174
- * get_ref ( future)
175
- }
176
-
177
- pub fn with < A , B > ( future : & Future < A > , blk : fn ( & A ) -> B ) -> B {
178
- //! Work with the value without copying it
179
-
180
- blk ( get_ref ( future) )
181
- }
182
-
183
143
#[ allow( non_implicitly_copyable_typarams) ]
184
144
pub mod test {
185
145
#[ test]
186
146
pub fn test_from_value ( ) {
187
147
let f = from_value ( ~"snail") ;
188
- assert get( & f ) == ~"snail";
148
+ assert f . get ( ) == ~"snail";
189
149
}
190
150
191
151
#[ test]
192
152
pub fn test_from_port ( ) {
193
153
let ( ch, po) = oneshot:: init ( ) ;
194
154
send_one ( move ch, ~"whale") ;
195
155
let f = from_port ( move po) ;
196
- assert get( & f ) == ~"whale";
156
+ assert f . get ( ) == ~"whale";
197
157
}
198
158
199
159
#[ test]
200
160
pub fn test_from_fn ( ) {
201
161
let f = from_fn ( || ~"brail") ;
202
- assert get( & f ) == ~"brail";
162
+ assert f . get ( ) == ~"brail";
203
163
}
204
164
205
165
#[ test]
@@ -208,50 +168,32 @@ pub mod test {
208
168
assert f. get ( ) == ~"fail";
209
169
}
210
170
211
- #[ test]
212
- pub fn test_with ( ) {
213
- let f = from_value ( ~"nail") ;
214
- assert with( & f, |v| copy * v) == ~"nail";
215
- }
216
-
217
171
#[ test]
218
172
pub fn test_get_ref_method ( ) {
219
173
let f = from_value ( 22 ) ;
220
174
assert * f. get_ref ( ) == 22 ;
221
175
}
222
176
223
- #[ test]
224
- pub fn test_get_ref_fn ( ) {
225
- let f = from_value ( 22 ) ;
226
- assert * get_ref ( & f) == 22 ;
227
- }
228
-
229
- #[ test]
230
- pub fn test_interface_with ( ) {
231
- let f = from_value ( ~"kale") ;
232
- assert f. with ( |v| copy * v) == ~"kale";
233
- }
234
-
235
177
#[ test]
236
178
pub fn test_spawn ( ) {
237
179
let f = spawn ( || ~"bale") ;
238
- assert get( & f ) == ~"bale";
180
+ assert f . get ( ) == ~"bale";
239
181
}
240
182
241
183
#[ test]
242
184
#[ should_fail]
243
185
#[ ignore( cfg( target_os = "win32" ) ) ]
244
186
pub fn test_futurefail ( ) {
245
187
let f = spawn ( || fail) ;
246
- let _x: ~str = get ( & f ) ;
188
+ let _x: ~str = f . get ( ) ;
247
189
}
248
190
249
191
#[ test]
250
192
pub fn test_sendable_future ( ) {
251
193
let expected = ~"schlorf";
252
194
let f = do spawn |copy expected| { copy expected } ;
253
195
do task:: spawn |move f, move expected| {
254
- let actual = get ( & f ) ;
196
+ let actual = f . get ( ) ;
255
197
assert actual == expected;
256
198
}
257
199
}
0 commit comments