Crates.io | funnel |
lib.rs | funnel |
version | 1.1.0 |
source | src |
created_at | 2016-06-09 01:21:46.955189 |
updated_at | 2016-06-09 14:03:11.668242 |
description | A simple data structure that makes it easy to read from multiple channels from a single source |
homepage | https://github.com/zsck/funnel-rs |
repository | https://github.com/zsck/funnel-rs |
max_upload_size | |
id | 5327 |
size | 11,621 |
Funnel is a container data structure for receivers for Rust channels. It accumulates receivers whose corresponding senders have been sent to other scopes/functions/threads and provides a simple interface for reading from each of them, eliminating the need to manually iterate through your own collection, read, and handle errors.
You can read the documentation
for the project to learn the entire (small) interface the crate presents through the
Funnel
struct.
extern crate funnel;
use funnel::Funnel;
use std::thread;
fn main () {
// Create a funnel and use it to produce some senders. It will automatically
// start handling the receivers corresponding to each writer if we use
// `add_receiver()`.
let mut fun = Funnel::new();
let writer1 = fun.add_receiver();
let writer2 = fun.add_receiver();
// Spin up a thread to write to each sender.
thread::spawn(move || {
let _ = writer1.send(32).unwrap();
});
thread::spawn(move || {
let _ = writer2.send(64).unwrap();
});
// We can now use the single funnel to read from both receivers, in the order
// that they were added to the funnel.
assert!(match fun.recv() {
(Some(read_value), errors) => read_value == 32 && errors.len() == 0,
_ => false,
});
assert!(match fun.recv() {
(Some(read_value), errors) => read_value == 64 && errors.len() == 0,
_ => false,
});
}