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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use super::Container;
use super::SimpleBufferTrigger;
use std::sync::{mpsc, Mutex, RwLock};
use std::{fmt, time::Duration};
pub struct Builder<E, C>
where
E: fmt::Debug,
{
name: String,
defalut_container: fn() -> C,
accumulator: fn(&mut C, E),
consumer: fn(C),
max_len: usize,
interval: Option<Duration>,
}
impl<E, C> fmt::Debug for Builder<E, C>
where
E: fmt::Debug,
C: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "name {}", self.name)
}
}
impl<E, C> Builder<E, C>
where
E: fmt::Debug,
C: fmt::Debug,
{
pub fn builder(defalut_container: fn() -> C) -> Self {
Self {
name: "anonymous".to_owned(),
defalut_container,
accumulator: |_, _| {},
consumer: |_| {},
max_len: std::usize::MAX,
interval: None,
}
}
pub fn name(mut self, name: String) -> Self {
self.name = name;
self
}
pub fn accumulator(mut self, accumulator: fn(&mut C, E) -> ()) -> Self {
self.accumulator = accumulator;
self
}
pub fn consumer(mut self, consumer: fn(C)) -> Self {
self.consumer = consumer;
self
}
pub fn max_len(mut self, max_len: usize) -> Self {
self.max_len = max_len;
self
}
pub fn interval(mut self, interval: Duration) -> Self {
self.interval = Some(interval);
self
}
pub fn build(self) -> SimpleBufferTrigger<E, C> {
let (sender, receiver) = mpsc::channel();
SimpleBufferTrigger {
name: self.name,
defalut_container: self.defalut_container,
container: RwLock::new(Container {
len: 0,
accumulator: self.accumulator,
container: (self.defalut_container)(),
clock: false,
}),
consumer: self.consumer,
max_len: self.max_len,
interval: self.interval,
sender: Mutex::new(sender),
receiver: Mutex::new(receiver),
}
}
}