Versust
A Rust library for structured concurrency patterns.
This library spawns threads for each job with various synchronization strategies.
Inspired from the Verse Programming language.
Install
Usage
We have a macro API and a function API for each function.
Macro API has a simpler syntax, with no need to declare Boxes or closures.
Roughly macro![[optional preprocessing section]{closure body}, ...] will be converted into:
macro
sync!
Waits for all jobs to complete and returns their results.
use sync;
use thread;
use Duration;
let results = sync!;
let results: = results.into_iter.map.collect;
assert_eq!;
race!
Waits for the fastest job to complete and returns its index and result. Remaining jobs are terminated.
To implement this feature, race! macro will injects a code for each semicolon-separated statement.
Since a job may terminate after any semicolon, it must guarantee that it does proper cleanup at any point of termination.
use race;
use Arc;
use ;
use thread;
use Duration;
let finished_count = new;
let result = race!;
assert_eq!;
assert_eq!;
// 1st job is terminated when 2nd job is finished, so finished_count should not increase
sleep;
assert_eq!;
rush!
Waits for the fastest job to complete and returns its index and result. Remaining jobs continue running in the background.
use rush;
use Arc;
use ;
use thread;
use Duration;
let finished_count = new;
let result = rush!;
assert_eq!;
assert_eq!;
// 1st job is still running in the background
sleep;
assert_eq!;
branch!
Executes jobs concurrently and immediately returns their JoinHandles.
use branch;
use thread;
use Duration;
let handles = branch!;
let results: = handles.into_iter.map.collect;
assert_eq!;
License
Licensed under either of:
- Apache License, Version 2.0, (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.