buffer_trigger/buffer_trigger_async/
simple.rs

1use super::general::{self, General};
2use lifetime_thread::Outer;
3use std::{fmt, mem, time::Duration};
4#[derive(Debug)]
5struct Payload<C>
6where
7    C: fmt::Debug,
8{
9    len: usize,
10    container: C,
11    defalut_container: fn() -> C,
12}
13
14pub struct Simple<E, C>
15where
16    E: fmt::Debug + Sync + Send + 'static,
17    C: fmt::Debug + Sync + Send + 'static,
18{
19    general: Outer<General<E, C, Payload<C>>>,
20}
21
22impl<E, C> Simple<E, C>
23where
24    E: fmt::Debug + Sync + Send,
25    C: fmt::Debug + Sync + Send,
26{
27    pub async fn is_empty(&self) -> bool {
28        self.general.is_empty().await
29    }
30    pub async fn len(&self) -> usize {
31        self.general.len().await
32    }
33    pub async fn push(&self, value: E) {
34        self.general.push(value).await
35    }
36    pub async fn trigger(&self) {
37        self.general.trigger().await
38    }
39    pub async fn listen_clock_trigger(&self) {
40        self.general.listen_clock_trigger().await
41    }
42}
43
44pub struct Builder<E, C>
45where
46    E: fmt::Debug,
47    C: fmt::Debug,
48{
49    name: String,
50    defalut_container: fn() -> C,
51    accumulator: fn(&mut C, E),
52    consumer: fn(C),
53    max_len: usize,
54    interval: Option<Duration>,
55}
56
57impl<E, C> fmt::Debug for Builder<E, C>
58where
59    E: fmt::Debug,
60    C: fmt::Debug,
61{
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        write!(f, "name {}", self.name)
64    }
65}
66
67impl<E, C> Builder<E, C>
68where
69    E: fmt::Debug + Sync + Send,
70    C: fmt::Debug + Sync + Send,
71{
72    /// init
73    pub fn builder(defalut_container: fn() -> C) -> Self {
74        Self {
75            name: "anonymous".to_owned(),
76            defalut_container,
77            accumulator: |_, _| {},
78            consumer: |_| {},
79            max_len: std::usize::MAX,
80            interval: None,
81        }
82    }
83
84    /// set `name`
85    #[must_use]
86    pub fn name(mut self, name: String) -> Self {
87        self.name = name;
88        self
89    }
90
91    /// set `accumulator`
92    pub fn accumulator(mut self, accumulator: fn(&mut C, E)) -> Self {
93        self.accumulator = accumulator;
94        self
95    }
96
97    /// set `consumer`
98    pub fn consumer(mut self, consumer: fn(C)) -> Self {
99        self.consumer = consumer;
100        self
101    }
102
103    /// set `max_len`
104    #[must_use]
105    pub fn max_len(mut self, max_len: usize) -> Self {
106        self.max_len = max_len;
107        self
108    }
109
110    /// set `interval`
111    #[must_use]
112    pub fn interval(mut self, interval: Duration) -> Self {
113        self.interval = Some(interval);
114        self
115    }
116
117    /// `build`
118    #[must_use]
119    pub fn build(self) -> Simple<E, C> {
120        let payload = Payload {
121            container: (self.defalut_container)(),
122            defalut_container: self.defalut_container,
123            len: 0,
124        };
125
126        let mut general = general::builder::Builder::builder();
127        if let Some(t) = self.interval {
128            general = general.interval(t);
129        }
130        let general = general
131            .consumer(self.consumer)
132            .max_len(self.max_len)
133            .payload(payload)
134            .get_len(|p| p.as_ref().unwrap().len)
135            .incr_len(|p| p.as_mut().unwrap().len += 1)
136            .clear_len(|p| p.as_mut().unwrap().len = 0)
137            .get_container(|p| &mut p.as_mut().unwrap().container)
138            .get_and_clear_container(|p| {
139                let mut new_container = (p.as_ref().unwrap().defalut_container)();
140                mem::swap(&mut new_container, &mut p.as_mut().unwrap().container);
141                new_container
142            })
143            .accumulator(self.accumulator)
144            .build();
145
146        Simple { general }
147    }
148}