pub struct AtomicSignedCount { /* private fields */ }Expand description
A signed atomic counter with synchronization-oriented operations.
Use this type when the counter models a delta, balance, backlog, offset, or other quantity that may legitimately cross zero. Examples include producer minus consumer deltas, permit debt, retry backlog changes, or accumulated scheduling offsets.
For counters that must never be negative, prefer
AtomicCount. For pure metrics or statistics,
prefer the regular atomic integer types such as
Atomic<isize>.
This counter never wraps. Operations that would overflow the signed range
panic. Use try_add or try_sub when
overflow is a normal business outcome.
§Example
use qubit_atomic::AtomicSignedCount;
let backlog_delta = AtomicSignedCount::zero();
assert_eq!(backlog_delta.add(5), 5);
assert_eq!(backlog_delta.sub(8), -3);
assert!(backlog_delta.is_negative());§Author
Haixing Hu
Implementations§
Source§impl AtomicSignedCount
impl AtomicSignedCount
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Sourcepub fn inc(&self) -> isize
pub fn inc(&self) -> isize
Increments the counter by one and returns the new value.
§Returns
The counter value after the increment.
§Panics
Panics if the increment would overflow isize::MAX.
§Example
use qubit_atomic::AtomicSignedCount;
let counter = AtomicSignedCount::zero();
assert_eq!(counter.inc(), 1);Sourcepub fn dec(&self) -> isize
pub fn dec(&self) -> isize
Decrements the counter by one and returns the new value.
§Returns
The counter value after the decrement.
§Panics
Panics if the decrement would underflow isize::MIN.
§Example
use qubit_atomic::AtomicSignedCount;
let counter = AtomicSignedCount::zero();
assert_eq!(counter.dec(), -1);Sourcepub fn add(&self, delta: isize) -> isize
pub fn add(&self, delta: isize) -> isize
Adds delta to the counter and returns the new value.
§Parameters
delta- The amount to add. It may be negative.
§Returns
The counter value after the addition.
§Panics
Panics if the addition would overflow or underflow the signed range.
§Example
use qubit_atomic::AtomicSignedCount;
let counter = AtomicSignedCount::new(2);
assert_eq!(counter.add(-5), -3);Sourcepub fn try_add(&self, delta: isize) -> Option<isize>
pub fn try_add(&self, delta: isize) -> Option<isize>
Tries to add delta to the counter.
§Parameters
delta- The amount to add. It may be negative.
§Returns
Some(new_value) if the addition succeeds, or None if it would
overflow or underflow the signed range. On None, the counter is left
unchanged.
§Example
use qubit_atomic::AtomicSignedCount;
let counter = AtomicSignedCount::new(-2);
assert_eq!(counter.try_add(5), Some(3));Sourcepub fn sub(&self, delta: isize) -> isize
pub fn sub(&self, delta: isize) -> isize
Subtracts delta from the counter and returns the new value.
§Parameters
delta- The amount to subtract. It may be negative.
§Returns
The counter value after the subtraction.
§Panics
Panics if the subtraction would overflow or underflow the signed range.
§Example
use qubit_atomic::AtomicSignedCount;
let counter = AtomicSignedCount::new(2);
assert_eq!(counter.sub(5), -3);Sourcepub fn try_sub(&self, delta: isize) -> Option<isize>
pub fn try_sub(&self, delta: isize) -> Option<isize>
Tries to subtract delta from the counter.
§Parameters
delta- The amount to subtract. It may be negative.
§Returns
Some(new_value) if the subtraction succeeds, or None if it would
overflow or underflow the signed range. On None, the counter is left
unchanged.
§Example
use qubit_atomic::AtomicSignedCount;
let counter = AtomicSignedCount::new(2);
assert_eq!(counter.try_sub(5), Some(-3));