#async #future #threadpool #rayon


Easily run closures on thread pools and turn their result into Futures

1 unstable release

0.1.0 Nov 4, 2021

#965 in Concurrency


54 lines

This crate makes it easy to transform functions or closures into two objects: one that can be run on a thread pool, and a Future on its result. Contrarily to other existing solution, starting the computation is independent from polling the future. It is entirely independent of both the thread or thread pool used to run the actual computations and the future executor.

The entry-point is closure_future.


Workers can be run on threads:

# use std::time::Duration;
# use closure_future::closure_future;
# use pollster::block_on;
let (future, worker) = closure_future(|| {
    // ... do some work and return a value ...
std::thread::spawn(|| worker.run());
assert_eq!(block_on(future), Ok("Hello!"))

Workers can also be run using rayon global thread-pool, using a provided helper function:

# use std::time::Duration;
# use closure_future::spawn_rayon;
# use pollster::block_on;
let mut futures = Vec::new();
for i in 0..10 {
    let future = spawn_rayon(move || {
        // ... do some work and return a value ...
for (i,fut) in futures.into_iter().enumerate() {