use super::{Accumulator, Collector};
pub const fn count() -> CountCollector {
CountCollector
}
pub struct CountCollector;
impl<Input> Collector<Input> for CountCollector
where
Input: Send + Sync,
{
type Value = ();
type Result = usize;
type Accumulator = CountAccumulator;
#[inline]
fn extract(&self, _input: Input) {}
fn create_accumulator(&self) -> Self::Accumulator {
CountAccumulator { count: 0 }
}
}
pub struct CountAccumulator {
count: usize,
}
impl CountAccumulator {
#[inline]
pub fn get(&self) -> usize {
self.count
}
}
impl Accumulator<(), usize> for CountAccumulator {
type Retraction = ();
#[inline]
fn accumulate(&mut self, _: ()) -> Self::Retraction {
self.count += 1;
}
#[inline]
fn retract(&mut self, _: Self::Retraction) {
self.count = self.count.saturating_sub(1);
}
#[inline]
fn with_result<T>(&self, f: impl FnOnce(&usize) -> T) -> T {
f(&self.count)
}
#[inline]
fn reset(&mut self) {
self.count = 0;
}
}