Skip to content

Commit 48d5b4b

Browse files
committed
Add Future::spawn_with
1 parent ff85389 commit 48d5b4b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/libextra/future.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ impl<A:Send> Future<A> {
139139

140140
Future::from_port(port)
141141
}
142+
143+
pub fn spawn_with<B: Send>(v: B, blk: ~fn(B) -> A) -> Future<A> {
144+
/*!
145+
* Create a future from a unique closure taking one argument.
146+
*
147+
* The closure and its argument will be moved into a new task. The
148+
* closure will be run and its result used as the value of the future.
149+
*/
150+
151+
let (port, chan) = oneshot();
152+
153+
do task::spawn_with((v, chan)) |(v, chan)| {
154+
chan.send(blk(v));
155+
}
156+
157+
Future::from_port(port)
158+
}
142159
}
143160

144161
#[cfg(test)]
@@ -193,6 +210,12 @@ mod test {
193210
assert_eq!(f.get(), ~"bale");
194211
}
195212
213+
#[test]
214+
fn test_spawn_with() {
215+
let mut f = Future::spawn_with(~"gale", |s| { s });
216+
assert_eq!(f.get(), ~"gale");
217+
}
218+
196219
#[test]
197220
#[should_fail]
198221
fn test_futurefail() {

0 commit comments

Comments
 (0)