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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use super::{General, Locker};
use lifetime_thread::Outer;
use std::sync::{mpsc, Mutex, RwLock};
use std::{fmt, time::Duration};
pub struct Builder<E, C, P>
where
P: fmt::Debug + Send,
E: fmt::Debug + Send,
C: fmt::Debug + Send,
{
payload: Option<P>,
name: String,
consumer: fn(C),
max_len: usize,
interval: Option<Duration>,
get_len: fn(&Option<P>) -> usize,
incr_len: fn(&mut Option<P>),
clear_len: fn(&mut Option<P>),
get_container: fn(&mut Option<P>) -> &mut C,
accumulator: fn(&mut C, E),
get_and_clear_container: fn(&mut Option<P>) -> C,
}
impl<E, C, P> fmt::Debug for Builder<E, C, P>
where
P: fmt::Debug + Send,
E: fmt::Debug + Send,
C: fmt::Debug + Send,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "name {}", self.name)
}
}
impl<E, C, P> Builder<E, C, P>
where
P: fmt::Debug + Send,
E: fmt::Debug + Send,
C: fmt::Debug + Send,
{
#[must_use]
pub fn builder() -> Self {
Self {
payload: None,
name: "anonymous".to_owned(),
get_len: |_| 1,
incr_len: |_| {},
clear_len: |_| {},
get_container: |_| panic!(),
accumulator: |_, _| {},
get_and_clear_container: |_| panic!(),
consumer: |_| {},
max_len: std::usize::MAX,
interval: None,
}
}
pub fn name(mut self, name: String) -> Self {
self.name = name;
self
}
pub fn get_len(mut self, get_len: fn(&Option<P>) -> usize) -> Self {
self.get_len = get_len;
self
}
pub fn incr_len(mut self, incr_len: fn(&mut Option<P>)) -> Self {
self.incr_len = incr_len;
self
}
pub fn clear_len(mut self, clear_len: fn(&mut Option<P>)) -> Self {
self.clear_len = clear_len;
self
}
pub fn get_container(mut self, get_container: fn(&mut Option<P>) -> &mut C) -> Self {
self.get_container = get_container;
self
}
pub fn get_and_clear_container(
mut self,
get_and_clear_container: fn(&mut Option<P>) -> C,
) -> Self {
self.get_and_clear_container = get_and_clear_container;
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 payload(mut self, payload: P) -> Self {
self.payload = Some(payload);
self
}
pub fn build(self) -> Outer<General<E, C, P>> {
let (sender, receiver) = mpsc::channel();
let general = General {
name: self.name,
locker: RwLock::new(Locker {
get_len: self.get_len,
incr_len: self.incr_len,
clear_len: self.clear_len,
get_container: self.get_container,
get_and_clear_container: self.get_and_clear_container,
accumulator: self.accumulator,
clock: false,
payload: self.payload,
}),
consumer: self.consumer,
max_len: self.max_len,
interval: self.interval,
sender: Mutex::new(sender),
receiver: Mutex::new(receiver),
};
if self.interval.is_some() {
lifetime_thread::spawn(general, |inner| {
while let Some(g) = inner.get() {
g.listen_clock_trigger()
}
})
} else {
lifetime_thread::spawn(general, |_| {})
}
}
}