Module desync::pipe

source ·
Expand description

Desync pipes provide a way to generate and process streams via a Desync object

Pipes are an excellent way to interface Desync objects and the futures library. Piping a stream into a Desync object is equivalent to spawning it with an executor, except without the need to dedicate a thread to running it.

There are two kinds of pipe. The pipe_in function creates a pipe that processes each value made available from a stream on a desync object as they arrive, producing no results. This is useful for cases where a Desync object is being used as the endpoint for some data processing (for example, to insert the results of an operation into an asynchronous database object).

The pipe function pipes data through an object. For every input value, it produces an output value. This is good for creating streams that perform some kind of asynchronous processing operation or that need to access data from inside a Desync object.

Here’s an example of using pipe_in to store data in a HashSet:

use futures::sync::mpsc;
use futures::executor;
use desync::*;
 
let desync_hashset      = Arc::new(Desync::new(HashSet::new()));
let (sender, receiver)  = mpsc::channel(5);
 
pipe_in(Arc::clone(&desync_hashset), receiver, |hashset, value| { value.map(|value| hashset.insert(value)); });
 
let mut sender = executor::spawn(sender);
sender.wait_send("Test".to_string());
sender.wait_send("Another value".to_string());

Structs

A stream generated by a pipe

Functions

Pipes a stream into this object. Whenever an item becomes available on the stream, the processing function is called asynchronously with the item that was received. The return value is placed onto the output stream.
Pipes a stream into a desync object. Whenever an item becomes available on the stream, the processing function is called asynchronously with the item that was received.