pipecircuit 0.0.23

Correct. Robust. Fast. Portable. Pick four.
Documentation
const _: () = assert!(core::mem::size_of::<usize>() <= core::mem::size_of::<u64>());

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct EvidenceCounter {
    total: u64,
    saturated: bool,
}

impl EvidenceCounter {
    pub const fn new() -> Self {
        Self {
            total: 0,
            saturated: false,
        }
    }

    pub const fn total(self) -> u64 {
        self.total
    }

    pub const fn saturated(self) -> bool {
        self.saturated
    }

    pub const fn valid(self) -> bool {
        !self.saturated
    }

    pub fn record(&mut self, increment: u64) {
        match self.total.checked_add(increment) {
            Some(total) => self.total = total,
            None => {
                self.total = u64::MAX;
                self.saturated = true;
            }
        }
    }

    pub fn record_usize(&mut self, increment: usize) {
        self.record(increment as u64);
    }

    pub fn merge(&mut self, other: Self) {
        self.record(other.total);
        self.saturated |= other.saturated;
    }

    pub fn reset(&mut self) {
        *self = Self::new();
    }
}