pub struct AtomicCount { /* private fields */ }Expand description
A non-negative atomic counter with synchronization-oriented operations.
Use this type when the counter value is part of a concurrent state machine, such as active task counts, in-flight request counts, resource usage counts, or shutdown and termination checks. Its operations return the value after the update, which makes zero-transition logic straightforward.
For pure metrics, statistics, event counters, or ID generation, prefer the
regular atomic integer types such as Atomic<usize>.
Those types keep arithmetic operations lightweight for pure counting.
This counter never wraps. Incrementing past usize::MAX panics, and
decrementing below zero panics. Use try_add,
try_dec, or try_sub when overflow or
underflow is a normal business outcome.
§Example
use qubit_atomic::AtomicCount;
let active_tasks = AtomicCount::zero();
active_tasks.inc();
assert!(!active_tasks.is_zero());
if active_tasks.dec() == 0 {
// The last active task finished; notify termination waiters here.
}Implementations§
Source§impl AtomicCount
impl AtomicCount
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Sourcepub fn inc(&self) -> usize
pub fn inc(&self) -> usize
Increments the counter by one and returns the new value.
§Returns
The counter value after the increment.
§Panics
Panics if the increment would overflow usize::MAX.
§Example
use qubit_atomic::AtomicCount;
let counter = AtomicCount::zero();
assert_eq!(counter.inc(), 1);Sourcepub fn add(&self, delta: usize) -> usize
pub fn add(&self, delta: usize) -> usize
Adds delta to the counter and returns the new value.
§Parameters
delta- The amount to add.
§Returns
The counter value after the addition.
§Panics
Panics if the addition would overflow usize::MAX.
§Example
use qubit_atomic::AtomicCount;
let counter = AtomicCount::new(2);
assert_eq!(counter.add(3), 5);Sourcepub fn try_add(&self, delta: usize) -> Option<usize>
pub fn try_add(&self, delta: usize) -> Option<usize>
Tries to add delta to the counter.
§Parameters
delta- The amount to add.
§Returns
Some(new_value) if the addition succeeds, or None if it would
overflow usize::MAX. On None, the counter is left unchanged.
§Example
use qubit_atomic::AtomicCount;
let counter = AtomicCount::new(2);
assert_eq!(counter.try_add(3), Some(5));Sourcepub fn dec(&self) -> usize
pub fn dec(&self) -> usize
Decrements the counter by one and returns the new value.
This method is useful for detecting the transition to zero:
use qubit_atomic::AtomicCount;
let counter = AtomicCount::new(1);
if counter.dec() == 0 {
// This call consumed the final counted item.
}§Returns
The counter value after the decrement.
§Panics
Panics if the current value is zero.
Sourcepub fn try_dec(&self) -> Option<usize>
pub fn try_dec(&self) -> Option<usize>
Tries to decrement the counter by one.
§Returns
Some(new_value) if the decrement succeeds, or None if the current
value is zero. On None, the counter is left unchanged.
§Example
use qubit_atomic::AtomicCount;
let counter = AtomicCount::new(1);
assert_eq!(counter.try_dec(), Some(0));
assert_eq!(counter.try_dec(), None);Sourcepub fn sub(&self, delta: usize) -> usize
pub fn sub(&self, delta: usize) -> usize
Subtracts delta from the counter and returns the new value.
§Parameters
delta- The amount to subtract.
§Returns
The counter value after the subtraction.
§Panics
Panics if the subtraction would make the counter negative.
§Example
use qubit_atomic::AtomicCount;
let counter = AtomicCount::new(5);
assert_eq!(counter.sub(2), 3);Sourcepub fn try_sub(&self, delta: usize) -> Option<usize>
pub fn try_sub(&self, delta: usize) -> Option<usize>
Tries to subtract delta from the counter.
§Parameters
delta- The amount to subtract.
§Returns
Some(new_value) if the subtraction succeeds, or None if it would
make the counter negative. On None, the counter is left unchanged.
§Example
use qubit_atomic::AtomicCount;
let counter = AtomicCount::new(3);
assert_eq!(counter.try_sub(2), Some(1));
assert_eq!(counter.try_sub(2), None);