[−][src]Module desync::pipe
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::future; use futures::channel::mpsc; use futures::executor; use futures::prelude::*; executor::block_on(async { let desync_hashset = Arc::new(Desync::new(HashSet::new())); let (mut sender, receiver) = mpsc::channel(5); pipe_in(Arc::clone(&desync_hashset), receiver, |hashset, value| { hashset.insert(value); future::ready(()).boxed() }); sender.send("Test".to_string()).await.unwrap(); sender.send("Another value".to_string()).await.unwrap(); });
Structs
PipeStream | A stream generated by a pipe |
Functions
pipe | 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. |
pipe_in | 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. |