@@ -32,7 +32,7 @@ use std::comm::{PortOne, oneshot};
3232use std:: task;
3333use std:: util:: replace;
3434
35- # [ doc = "The future type" ]
35+ /// A type encapsulating the result of a computation which may not be complete
3636pub struct Future < A > {
3737 priv state : FutureState < A > ,
3838}
@@ -62,156 +62,148 @@ impl<A> Future<A> {
6262 _ => fail ! ( "Logic error." ) ,
6363 }
6464 }
65- }
6665
67- impl < A > Future < A > {
6866 pub fn get_ref < ' a > ( & ' a mut self ) -> & ' a A {
6967 /*!
7068 * Executes the future's closure and then returns a borrowed
7169 * pointer to the result. The borrowed pointer lasts as long as
7270 * the future.
7371 */
74- unsafe {
75- {
76- match self . state {
77- Forced ( ref mut v) => { return cast:: transmute ( v) ; }
78- Evaluating => fail ! ( "Recursive forcing of future!" ) ,
79- Pending ( _) => { }
80- }
81- }
82- {
83- let state = replace ( & mut self . state , Evaluating ) ;
84- match state {
72+ match self . state {
73+ Forced ( ref v) => return v,
74+ Evaluating => fail ! ( "Recursive forcing of future!" ) ,
75+ Pending ( _) => {
76+ match replace ( & mut self . state , Evaluating ) {
8577 Forced ( _) | Evaluating => fail ! ( "Logic error." ) ,
8678 Pending ( f) => {
8779 self . state = Forced ( f ( ) ) ;
88- cast :: transmute ( self . get_ref ( ) )
80+ self . get_ref ( )
8981 }
9082 }
9183 }
9284 }
9385 }
94- }
9586
96- pub fn from_value < A > ( val : A ) -> Future < A > {
97- /*!
98- * Create a future from a value.
99- *
100- * The value is immediately available and calling `get` later will
101- * not block.
102- */
103-
104- Future { state : Forced ( val) }
105- }
87+ pub fn from_value ( val : A ) -> Future < A > {
88+ /*!
89+ * Create a future from a value.
90+ *
91+ * The value is immediately available and calling `get` later will
92+ * not block.
93+ */
10694
107- pub fn from_port < A : Send > ( port : PortOne < A > ) -> Future < A > {
108- /*!
109- * Create a future from a port
110- *
111- * The first time that the value is requested the task will block
112- * waiting for the result to be received on the port.
113- */
95+ Future { state : Forced ( val) }
96+ }
11497
115- let port = Cell :: new ( port) ;
116- do from_fn {
117- port. take ( ) . recv ( )
98+ pub fn from_fn ( f : ~fn ( ) -> A ) -> Future < A > {
99+ /*!
100+ * Create a future from a function.
101+ *
102+ * The first time that the value is requested it will be retrieved by
103+ * calling the function. Note that this function is a local
104+ * function. It is not spawned into another task.
105+ */
106+
107+ Future { state : Pending ( f) }
118108 }
119109}
120110
121- pub fn from_fn < A > ( f : ~fn ( ) -> A ) -> Future < A > {
122- /*!
123- * Create a future from a function.
124- *
125- * The first time that the value is requested it will be retrieved by
126- * calling the function. Note that this function is a local
127- * function. It is not spawned into another task.
128- */
111+ impl < A : Send > Future < A > {
112+ pub fn from_port ( port : PortOne < A > ) -> Future < A > {
113+ /*!
114+ * Create a future from a port
115+ *
116+ * The first time that the value is requested the task will block
117+ * waiting for the result to be received on the port.
118+ */
119+
120+ let port = Cell :: new ( port) ;
121+ do Future :: from_fn {
122+ port. take ( ) . recv ( )
123+ }
124+ }
129125
130- Future { state : Pending ( f) }
131- }
126+ pub fn spawn ( blk : ~fn ( ) -> A ) -> Future < A > {
127+ /*!
128+ * Create a future from a unique closure.
129+ *
130+ * The closure will be run in a new task and its result used as the
131+ * value of the future.
132+ */
132133
133- pub fn spawn < A : Send > ( blk : ~fn ( ) -> A ) -> Future < A > {
134- /*!
135- * Create a future from a unique closure.
136- *
137- * The closure will be run in a new task and its result used as the
138- * value of the future.
139- */
134+ let ( port, chan) = oneshot ( ) ;
140135
141- let ( port, chan) = oneshot ( ) ;
136+ do task:: spawn_with ( chan) |chan| {
137+ chan. send ( blk ( ) ) ;
138+ }
142139
143- let chan = Cell :: new ( chan) ;
144- do task:: spawn {
145- let chan = chan. take ( ) ;
146- chan. send ( blk ( ) ) ;
140+ Future :: from_port ( port)
147141 }
148-
149- return from_port ( port) ;
150142}
151143
152144#[ cfg( test) ]
153145mod test {
154- use future:: * ;
146+ use future:: Future ;
155147
156148 use std:: cell:: Cell ;
157149 use std:: comm:: oneshot;
158150 use std:: task;
159151
160152 #[ test]
161153 fn test_from_value ( ) {
162- let mut f = from_value ( ~"snail") ;
154+ let mut f = Future :: from_value ( ~"snail") ;
163155 assert_eq ! ( f. get( ) , ~"snail");
164156 }
165157
166158 #[test]
167159 fn test_from_port() {
168160 let (po, ch) = oneshot();
169161 ch.send(~" whale");
170- let mut f = from_port(po);
162+ let mut f = Future:: from_port(po);
171163 assert_eq!(f.get(), ~" whale");
172164 }
173165
174166 #[test]
175167 fn test_from_fn() {
176- let mut f = from_fn(|| ~" brail");
168+ let mut f = Future:: from_fn(|| ~" brail");
177169 assert_eq!(f.get(), ~" brail");
178170 }
179171
180172 #[test]
181173 fn test_interface_get() {
182- let mut f = from_value(~" fail");
174+ let mut f = Future:: from_value(~" fail");
183175 assert_eq!(f.get(), ~" fail");
184176 }
185177
186178 #[test]
187179 fn test_interface_unwrap() {
188- let f = from_value(~" fail");
180+ let f = Future:: from_value(~" fail");
189181 assert_eq!(f.unwrap(), ~" fail");
190182 }
191183
192184 #[test]
193185 fn test_get_ref_method() {
194- let mut f = from_value(22);
186+ let mut f = Future:: from_value(22);
195187 assert_eq!(*f.get_ref(), 22);
196188 }
197189
198190 #[test]
199191 fn test_spawn() {
200- let mut f = spawn(|| ~" bale");
192+ let mut f = Future:: spawn(|| ~" bale");
201193 assert_eq!(f.get(), ~" bale");
202194 }
203195
204196 #[test]
205197 #[should_fail]
206198 fn test_futurefail() {
207- let mut f = spawn(|| fail!());
199+ let mut f = Future:: spawn(|| fail!());
208200 let _x: ~str = f.get();
209201 }
210202
211203 #[test]
212204 fn test_sendable_future() {
213205 let expected = " schlorf" ;
214- let f = Cell :: new( do spawn { expected } ) ;
206+ let f = Cell :: new( do Future :: spawn { expected } ) ;
215207 do task:: spawn {
216208 let mut f = f. take( ) ;
217209 let actual = f. get( ) ;
0 commit comments