Crate closure_future

Source
Expand description

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.

§Examples

Workers can be run on threads:

let (future, worker) = closure_future(|| {
    // ... do some work and return a value ...
    "Hello!"
});
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:

let mut futures = Vec::new();
for i in 0..10 {
    let future = spawn_rayon(move || {
        // ... do some work and return a value ...
        i
    });
    futures.push(future)
}
for (i,fut) in futures.into_iter().enumerate() {
    assert_eq!(block_on(fut),Ok(i));
}

Structs§

ClosureFuture
A Future whose output will be the output of the closure
ClosureFutureWorker
A worker to ractually run the closure, using self.run()

Enums§

RunError
The error type

Functions§

closure_future
Wraps a closure to be run as a Future
spawn_rayonrayon
An helper function to spawn a closure on the rayon global threadpool. Requires feature “rayon” to be activated.