Desync
[]
= "0.2"
Desync provides a single type, Desync<T>
that can be used to replace both threads and mutexes.
This type schedules operations for a contained data structure so that they are always performed
in order and optionally in the background.
Such a Desync
object can be created like so:
use Desync;
let number = new;
It supports two main operations. async
will schedule a new job for the object that will run
in a background thread. It's useful for deferring long-running operations and moving updates
so they can run in parallel.
let number = new;
number.async;
// We can carry on what we're doing with the update now running in the background
The other operation is sync
, which schedules a job to run synchronously on the data structure.
This is useful for retrieving values from a Desync
.
let new_number = number.sync; // = 42
Desync
objects always run operations in the order that is provided, so all operations are
serialized from the point of view of the data that they contain. When combined with the ability
to perform operations asynchronously, this provides a useful way to immediately parallelize
long-running operations.
Working with futures
Desync has support for the futures
library. The simplest operation is future()
, which creates
a future that runs asynchronously on a Desync
object but - unlike async()
can return a result.
It works like this:
let future_number = number.future;
assert!;
There is also support for streams, via the pipe_in()
and pipe()
functions. These work on
Arc<Desync<T>>
references and provide a way to process a stream asynchronously. These two
functions provide a powerful way to process input and also to connect Desync
objects together
using message-passing for communication.
let some_object = new;
pipe_in;
let output_stream = pipe;