composable_indexes/aggregation/
count.rs

1use crate::{
2    Index, ShallowClone,
3    core::{Insert, Remove, Seal, Update},
4};
5
6/// An index that only counts the number of items.
7#[derive(Clone)]
8pub struct Count {
9    count: usize,
10}
11
12impl Default for Count {
13    fn default() -> Self {
14        Self::new()
15    }
16}
17
18impl Count {
19    pub fn new() -> Self {
20        Count { count: 0 }
21    }
22}
23
24impl<_K> Index<_K> for Count {
25    #[inline]
26    fn insert(&mut self, _seal: Seal, _op: &Insert<_K>) {
27        self.count += 1;
28    }
29
30    #[inline]
31    fn remove(&mut self, _seal: Seal, _op: &Remove<_K>) {
32        self.count -= 1;
33    }
34
35    #[inline]
36    fn update(&mut self, _seal: Seal, _op: &Update<_K>) {
37        // No change in count on update
38    }
39}
40
41impl Count {
42    #[inline]
43    pub fn count(&self) -> usize {
44        self.count
45    }
46}
47
48impl ShallowClone for Count {}