vector_stream/batcher/
data.rs1pub trait BatchData<T> {
2 type Batch;
3
4 fn len(&self) -> usize;
6
7 fn take_batch(&mut self) -> Self::Batch;
9
10 fn push_item(&mut self, item: T);
12
13 fn is_empty(&self) -> bool {
14 self.len() == 0
15 }
16}
17
18impl<T> BatchData<T> for Vec<T> {
19 type Batch = Self;
20
21 fn len(&self) -> usize {
22 self.len()
23 }
24 fn take_batch(&mut self) -> Self::Batch {
25 std::mem::take(self)
26 }
27 fn push_item(&mut self, item: T) {
28 self.push(item);
29 }
30}
31
32pub struct BatchReduce<F, S> {
33 reducer: F,
34 state: S,
35 len: usize,
36}
37impl<F, S> BatchReduce<F, S>
38where
39 S: Default,
40{
41 pub fn new(reducer: F) -> BatchReduce<F, S> {
42 BatchReduce {
43 reducer,
44 state: S::default(),
45 len: 0,
46 }
47 }
48}
49impl<F, S, T> BatchData<T> for BatchReduce<F, S>
50where
51 F: FnMut(&mut S, T),
52 S: Default,
53{
54 type Batch = S;
55
56 fn len(&self) -> usize {
57 self.len
58 }
59
60 fn take_batch(&mut self) -> Self::Batch {
61 self.len = 0;
62 std::mem::take(&mut self.state)
63 }
64
65 fn push_item(&mut self, item: T) {
66 self.len += 1;
67 (self.reducer)(&mut self.state, item);
68 }
69}