use std::marker::PhantomData;
use super::{Accumulator, UniCollector};
pub fn count<A>() -> CountCollector<A> {
CountCollector {
_phantom: PhantomData,
}
}
pub struct CountCollector<A> {
_phantom: PhantomData<fn(&A)>,
}
impl<A> UniCollector<A> for CountCollector<A>
where
A: Send + Sync,
{
type Value = ();
type Result = usize;
type Accumulator = CountAccumulator;
#[inline]
fn extract(&self, _entity: &A) {}
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 {
#[inline]
fn accumulate(&mut self, _: &()) {
self.count += 1;
}
#[inline]
fn retract(&mut self, _: &()) {
self.count = self.count.saturating_sub(1);
}
#[inline]
fn finish(&self) -> usize {
self.count
}
#[inline]
fn reset(&mut self) {
self.count = 0;
}
}