Add Future::spawn_with

This commit is contained in:
Steven Fackler 2013-09-17 23:56:02 -07:00
parent ff85389344
commit 48d5b4b8e1
1 changed files with 23 additions and 0 deletions

View File

@ -139,6 +139,23 @@ impl<A:Send> Future<A> {
Future::from_port(port)
}
pub fn spawn_with<B: Send>(v: B, blk: ~fn(B) -> A) -> Future<A> {
/*!
* Create a future from a unique closure taking one argument.
*
* The closure and its argument will be moved into a new task. The
* closure will be run and its result used as the value of the future.
*/
let (port, chan) = oneshot();
do task::spawn_with((v, chan)) |(v, chan)| {
chan.send(blk(v));
}
Future::from_port(port)
}
}
#[cfg(test)]
@ -193,6 +210,12 @@ mod test {
assert_eq!(f.get(), ~"bale");
}
#[test]
fn test_spawn_with() {
let mut f = Future::spawn_with(~"gale", |s| { s });
assert_eq!(f.get(), ~"gale");
}
#[test]
#[should_fail]
fn test_futurefail() {