diff options
| author | Steven Fackler <sfackler@gmail.com> | 2013-09-17 23:56:02 -0700 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2013-09-19 15:19:25 -0700 |
| commit | 48d5b4b8e163a4eb4a8290bfed6786b333f49117 (patch) | |
| tree | 1e5c193f73b5a5cfe50e881a46a07c04be03ba76 /src/libextra | |
| parent | ff85389344d6fe4a318559b66b97c24b8fddf1e4 (diff) | |
| download | rust-48d5b4b8e163a4eb4a8290bfed6786b333f49117.tar.gz rust-48d5b4b8e163a4eb4a8290bfed6786b333f49117.zip | |
Add Future::spawn_with
Diffstat (limited to 'src/libextra')
| -rw-r--r-- | src/libextra/future.rs | 23 |
1 files changed, 23 insertions, 0 deletions
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<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)] @@ -194,6 +211,12 @@ mod test { } #[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() { let mut f = Future::spawn(|| fail!()); |
