1
0
Fork 0
forked from wry/wry
wry/src/utils/threshold_counter.rs
2024-02-14 18:33:17 +01:00

28 lines
512 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
}
}