1
0
Fork 0
forked from wry/wry
wry/src/utils/threshold_counter.rs
2025-02-21 10:44:29 +01:00

24 lines
472 B
Rust

use crate::utils::numcell::NumCell;
#[derive(Default)]
pub struct ThresholdCounter {
counter: NumCell<usize>,
}
impl ThresholdCounter {
pub fn inc(&self) -> bool {
self.counter.fetch_add(1) == 0
}
pub fn dec(&self) -> bool {
self.counter.fetch_sub(1) == 1
}
pub fn adj(&self, inc: bool) -> bool {
if inc { self.inc() } else { self.dec() }
}
pub fn active(&self) -> bool {
self.counter.get() > 0
}
}