Skip to content

Commit 0dc6e25

Browse files
committed
Add CpuPool::spawn_fn
Closes #118
1 parent 045ebfd commit 0dc6e25

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

futures-cpupool/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
4848
use std::thread;
4949

5050
use crossbeam::sync::MsQueue;
51-
use futures::{Future, oneshot, Oneshot, Complete, Poll};
51+
use futures::{IntoFuture, Future, oneshot, Oneshot, Complete, Poll};
5252
use futures::task::{self, Run, Executor};
5353

5454
/// A thread pool intended to run CPU intensive work.
@@ -165,6 +165,25 @@ impl CpuPool {
165165
task::spawn(sender).execute(self.inner.clone());
166166
CpuFuture { inner: rx }
167167
}
168+
169+
/// Spawns a closure on this thread pool.
170+
///
171+
/// This function is a convenience wrapper around the `spawn` function above
172+
/// for running a closure wrapped in `futures::lazy`. It will spawn the
173+
/// function `f` provided onto the thread pool, and continue to run the
174+
/// future returned by `f` on the thread pool as well.
175+
///
176+
/// The returned future will be a handle to the result produced by the
177+
/// future that `f` returns.
178+
pub fn spawn_fn<F, R>(&self, f: F) -> CpuFuture<R::Item, R::Error>
179+
where F: FnOnce() -> R + Send + 'static,
180+
R: IntoFuture + 'static,
181+
R::Future: Send + 'static,
182+
R::Item: Send + 'static,
183+
R::Error: Send + 'static,
184+
{
185+
self.spawn(futures::lazy(f))
186+
}
168187
}
169188

170189
fn work(inner: &Inner) {

0 commit comments

Comments
 (0)