88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
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-
1611/*!
1712 * A type representing values that may be computed concurrently and
1813 * operations for working with them.
@@ -44,27 +39,42 @@ impl<A> Future<A> : Drop {
4439priv enum FutureState < A > {
4540 Pending ( fn ~( ) -> A ) ,
4641 Evaluating ,
47- Forced ( ~ A )
42+ Forced ( A )
4843}
4944
5045/// Methods on the `future` type
5146impl < A : Copy > Future < A > {
5247 fn get ( ) -> A {
5348 //! Get the value of the future
54-
55- get ( & self )
49+ * ( self . get_ref ( ) )
5650 }
5751}
5852
5953impl < 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
6654
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+ }
6878 }
6979}
7080
@@ -76,7 +86,7 @@ pub fn from_value<A>(val: A) -> Future<A> {
7686 * not block.
7787 */
7888
79- Future { state : Forced ( ~ ( move val) ) }
89+ Future { state : Forced ( move val) }
8090}
8191
8292pub fn from_port < A : Send > ( port : PortOne < A > ) ->
@@ -130,76 +140,26 @@ pub fn spawn<A:Send>(blk: fn~() -> A) -> Future<A> {
130140 return from_port ( move port) ;
131141}
132142
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-
183143#[ allow( non_implicitly_copyable_typarams) ]
184144pub mod test {
185145 #[ test]
186146 pub fn test_from_value ( ) {
187147 let f = from_value ( ~"snail") ;
188- assert get( & f ) == ~"snail";
148+ assert f . get ( ) == ~"snail";
189149 }
190150
191151 #[ test]
192152 pub fn test_from_port ( ) {
193153 let ( ch, po) = oneshot:: init ( ) ;
194154 send_one ( move ch, ~"whale") ;
195155 let f = from_port ( move po) ;
196- assert get( & f ) == ~"whale";
156+ assert f . get ( ) == ~"whale";
197157 }
198158
199159 #[ test]
200160 pub fn test_from_fn ( ) {
201161 let f = from_fn ( || ~"brail") ;
202- assert get( & f ) == ~"brail";
162+ assert f . get ( ) == ~"brail";
203163 }
204164
205165 #[ test]
@@ -208,50 +168,32 @@ pub mod test {
208168 assert f. get ( ) == ~"fail";
209169 }
210170
211- #[ test]
212- pub fn test_with ( ) {
213- let f = from_value ( ~"nail") ;
214- assert with( & f, |v| copy * v) == ~"nail";
215- }
216-
217171 #[ test]
218172 pub fn test_get_ref_method ( ) {
219173 let f = from_value ( 22 ) ;
220174 assert * f. get_ref ( ) == 22 ;
221175 }
222176
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-
235177 #[ test]
236178 pub fn test_spawn ( ) {
237179 let f = spawn ( || ~"bale") ;
238- assert get( & f ) == ~"bale";
180+ assert f . get ( ) == ~"bale";
239181 }
240182
241183 #[ test]
242184 #[ should_fail]
243185 #[ ignore( cfg( target_os = "win32" ) ) ]
244186 pub fn test_futurefail ( ) {
245187 let f = spawn ( || fail) ;
246- let _x: ~str = get ( & f ) ;
188+ let _x: ~str = f . get ( ) ;
247189 }
248190
249191 #[ test]
250192 pub fn test_sendable_future ( ) {
251193 let expected = ~"schlorf";
252194 let f = do spawn |copy expected| { copy expected } ;
253195 do task:: spawn |move f, move expected| {
254- let actual = get ( & f ) ;
196+ let actual = f . get ( ) ;
255197 assert actual == expected;
256198 }
257199 }
0 commit comments