@@ -29,7 +29,7 @@ use core::task;
29
29
30
30
#[ doc = "The future type" ]
31
31
pub struct Future < A > {
32
- priv mut state : FutureState < A > ,
32
+ priv state : FutureState < A > ,
33
33
}
34
34
35
35
// FIXME(#2829) -- futures should not be copyable, because they close
@@ -47,33 +47,68 @@ priv enum FutureState<A> {
47
47
48
48
/// Methods on the `future` type
49
49
pub impl < A : Copy > Future < A > {
50
- fn get ( & self ) -> A {
50
+ fn get ( & mut self ) -> A {
51
51
//! Get the value of the future
52
52
* ( self . get_ref ( ) )
53
53
}
54
54
}
55
55
56
56
pub impl < A > Future < A > {
57
+ #[ cfg( stage0) ]
57
58
fn get_ref< ' a > ( & ' a self ) -> & ' a A {
58
59
/*!
59
60
* Executes the future's closure and then returns a borrowed
60
61
* pointer to the result. The borrowed pointer lasts as long as
61
62
* the future.
62
63
*/
63
64
unsafe {
64
- match self . state {
65
- Forced ( ref mut v) => { return cast:: transmute ( v) ; }
66
- Evaluating => fail ! ( ~"Recursive forcing of future!"),
67
- Pending(_) => {}
65
+ {
66
+ match self . state {
67
+ Forced ( ref mut v) => { return cast:: transmute ( v) ; }
68
+ Evaluating => fail ! ( ~"Recursive forcing of future!"),
69
+ Pending(_) => {}
70
+ }
71
+ }
72
+ {
73
+ let mut state = Evaluating;
74
+ self.state <-> state;
75
+ match state {
76
+ Forced(_) | Evaluating => fail!(~" Logic error. "),
77
+ Pending(f) => {
78
+ self.state = Forced(f());
79
+ cast::transmute(self.get_ref())
80
+ }
81
+ }
68
82
}
83
+ }
84
+ }
69
85
70
- let mut state = Evaluating;
71
- self.state <-> state;
72
- match state {
73
- Forced(_) | Evaluating => fail!(~" Logic error. "),
74
- Pending(f) => {
75
- self.state = Forced(f());
76
- self.get_ref()
86
+ #[cfg(stage1)]
87
+ #[cfg(stage2)]
88
+ #[cfg(stage3)]
89
+ fn get_ref<'a>(&'a mut self) -> &'a A {
90
+ /*!
91
+ * Executes the future's closure and then returns a borrowed
92
+ * pointer to the result. The borrowed pointer lasts as long as
93
+ * the future.
94
+ */
95
+ unsafe {
96
+ {
97
+ match self.state {
98
+ Forced(ref mut v) => { return cast::transmute(v); }
99
+ Evaluating => fail!(~" Recursive forcing of future!"),
100
+ Pending(_) => {}
101
+ }
102
+ }
103
+ {
104
+ let mut state = Evaluating;
105
+ self.state <-> state;
106
+ match state {
107
+ Forced(_) | Evaluating => fail!(~" Logic error. "),
108
+ Pending(f) => {
109
+ self.state = Forced(f());
110
+ cast::transmute(self.get_ref())
111
+ }
77
112
}
78
113
}
79
114
}
@@ -150,54 +185,54 @@ mod test {
150
185
151
186
#[test]
152
187
fn test_from_value() {
153
- let f = from_value(~" snail");
188
+ let mut f = from_value(~" snail");
154
189
assert!(f.get() == ~" snail");
155
190
}
156
191
157
192
#[test]
158
193
fn test_from_port() {
159
194
let (po, ch) = oneshot();
160
195
send_one(ch, ~" whale");
161
- let f = from_port(po);
196
+ let mut f = from_port(po);
162
197
assert!(f.get() == ~" whale");
163
198
}
164
199
165
200
#[test]
166
201
fn test_from_fn() {
167
- let f = from_fn(|| ~" brail");
202
+ let mut f = from_fn(|| ~" brail");
168
203
assert!(f.get() == ~" brail");
169
204
}
170
205
171
206
#[test]
172
207
fn test_interface_get() {
173
- let f = from_value(~" fail");
208
+ let mut f = from_value(~" fail");
174
209
assert!(f.get() == ~" fail");
175
210
}
176
211
177
212
#[test]
178
213
fn test_get_ref_method() {
179
- let f = from_value(22);
214
+ let mut f = from_value(22);
180
215
assert!(*f.get_ref() == 22);
181
216
}
182
217
183
218
#[test]
184
219
fn test_spawn() {
185
- let f = spawn(|| ~" bale");
220
+ let mut f = spawn(|| ~" bale");
186
221
assert!(f.get() == ~" bale");
187
222
}
188
223
189
224
#[test]
190
225
#[should_fail]
191
226
#[ignore(cfg(target_os = " win32"))]
192
227
fn test_futurefail() {
193
- let f = spawn(|| fail!());
228
+ let mut f = spawn(|| fail!());
194
229
let _x: ~str = f.get();
195
230
}
196
231
197
232
#[test]
198
233
fn test_sendable_future() {
199
234
let expected = ~" schlorf" ;
200
- let f = do spawn { copy expected } ;
235
+ let mut f = do spawn { copy expected } ;
201
236
do task:: spawn || {
202
237
let actual = f. get( ) ;
203
238
assert!( actual == expected) ;
0 commit comments