#receiver #channel #structure #collection

funnel

A simple data structure that makes it easy to read from multiple channels from a single source

3 stable releases

Uses old Rust 2015

1.1.0 Jun 9, 2016
1.0.1 Jun 9, 2016

#1159 in Data structures

MIT license

9KB
122 lines

Funnel

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.

Example

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,
    });
}

No runtime deps