union!
union! - one macro to rule them all. Combines sync/async results, transforms tuple of results in result of tuple, provides single and multi thread (sync/async) step by step execution of branches and useful shortcut combinators.
Combinators
-
Map:
|>expr - value.map(expr) -
AndThen:
=>expr - value.and_then(expr), -
Then:
->expr - expr(value) -
Dot:
>.expr - value.expr -
Or:
<|expr - value.or(expr) -
OrElse:
<=expr - value.or_else(expr) -
MapErr:
!>expr - value.map_err(expr) -
Inspect:
?>expr - (|value| { expr(value); value })(value)
where value is the previous value.
Every combinator prefixed by ~ will act as deferred action (all actions will wait until completion in every step and only after move to the next one).
Handler
might be one of
-
map=> will act as results.map(|(result0, result1, ..)| handler(result0, result1, ..)) -
and_then=> will act as results.and_then(|(result0, result1, ..)| handler(result0, result1, ..)) -
then=> will act as handler(result0, result1, ..)
or not specified - then Result<(result0, result1, ..), Error> or Option<(result0, result1, ..)> will be returned.
Single thread combinations
Simple sync results combination
Converts input in series of chained results and joins them step by step.
extern crate union;
use Error;
use union;
type Result<T> = Result;
Async results combination
Each branch will represent chain of tasks. All branches will be joined using join! macro and macro will return unpolled future.
extern crate union;
extern crate futures;
extern crate tokio;
use Error;
use union_async;
use ;
type Result<T> = Result;
async
async
async
Multi-thread combinations
To execute several tasks in parallel you could use union_spawn! (spawnion!) for sync tasks
and union_async_spawn! (spasyncion!) for async tasks. Since union_async already provides parallel futures execution in one thread, union_async_spawn! spawns every branch into tokio executor so they will be evaluated in multi-threaded executor.
Multi-thread sync branches
union_spawn spawns one ::std::thread per each step of each branch (number of branches is the max thread count at the time).
extern crate union;
use Error;
use union_spawn;
type Result<T> = Result;
union_async_spawn! uses ::tokio::spawn function to spawn tasks so it should be done inside tokio runtime
(number of branches is the max count of tokio tasks at the time).
Multi-thread async branches
extern crate union;
extern crate futures;
extern crate tokio;
use Error;
use union_async_spawn;
use ;
type Result<T> = Result;
async
async
async
Using combinators we can rewrite first example like
extern crate union;
use Error;
use union;
type Result<T> = Result;
By separating chain in actions, you will make actions wait for completion of all of them in current step before go to the next step.
extern crate union;
use Error;
use union;
type Result<T> = Result;