forked from valebes/ppl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_filter.rs
50 lines (45 loc) · 1.04 KB
/
test_filter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Pipeline with a filter node.
*/
use ppl::{
prelude::*,
templates::misc::{Filter, SinkVec},
};
// Source node.
struct Source {
streamlen: usize,
counter: usize,
}
impl Out<usize> for Source {
fn run(&mut self) -> Option<usize> {
if self.counter < self.streamlen {
self.counter += 1;
Some(self.counter)
} else {
None
}
}
}
// Filter function.
fn is_even(input: &usize) -> bool {
input % 2 == 0
}
#[test]
fn test_filter() {
env_logger::init();
let mut p = pipeline![
Source {
streamlen: 100,
counter: 0
},
// We can create a filter node with the filter template.
Filter::build(|el: &usize| -> bool { is_even(el) }),
// Also here we can use templates. In this case we use the SinkVec template.
SinkVec::build()
];
// Start the pipeline.
p.start();
// Wait for the pipeline to finish and collect the results.
let res = p.wait_end().unwrap();
assert_eq!(res.len(), 50);
}