From 48d5b4b8e163a4eb4a8290bfed6786b333f49117 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 17 Sep 2013 23:56:02 -0700 Subject: [PATCH] Add Future::spawn_with --- src/libextra/future.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libextra/future.rs b/src/libextra/future.rs index 2d68cca4adc..40ed56855f2 100644 --- a/src/libextra/future.rs +++ b/src/libextra/future.rs @@ -139,6 +139,23 @@ impl Future { Future::from_port(port) } + + pub fn spawn_with(v: B, blk: ~fn(B) -> A) -> Future { + /*! + * 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() {