use super::{General, Locker};
use async_std::sync::{self, Mutex, RwLock};
use std::{fmt, time::Duration};
pub struct Builder<E, C, P>
where
P: fmt::Debug,
E: fmt::Debug,
C: fmt::Debug,
{
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 + Sync + Send,
E: fmt::Debug + Sync + Send,
C: fmt::Debug + Sync + 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 + Sync + Send,
E: fmt::Debug + Sync + Send,
C: fmt::Debug + Sync + 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) -> General<E, C, P> {
let (sender, receiver) = sync::channel(10);
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),
}
}
}