3 个稳定版本
使用旧的 Rust 2015
1.1.0 | 2016 年 6 月 9 日 |
---|---|
1.0.1 | 2016 年 6 月 9 日 |
#1208 在 数据结构
9KB
122 行
漏斗
漏斗是 Rust 通道接收器的容器数据结构。它累积已经发送到其他作用域/函数/线程的接收器,为从每个接收器中读取提供简单的接口,从而消除手动遍历自己的集合、读取和处理错误的需求。
您可以阅读项目的文档,了解通过 Funnel
结构体提供的整个(小巧)接口 [点击这里](https://zsck.github.io/projects/funnel/doc/funnel/index.html)。
示例
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,
});
}