Skip to main content

AtomicCounter

Struct AtomicCounter 

Source
pub struct AtomicCounter { /* 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 AtomicUsize. 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::AtomicCounter;

let active_tasks = AtomicCounter::zero();

active_tasks.inc();
assert!(!active_tasks.is_zero());

if active_tasks.dec() == 0 {
    // The last active task finished; notify termination waiters here.
}

§Author

Haixing Hu

Implementations§

Source§

impl AtomicCounter

Source

pub const fn new(value: usize) -> Self

Creates a new non-negative atomic counter.

§Parameters
  • value - The initial counter value.
§Returns

A counter initialized to value.

§Example
use qubit_atomic::AtomicCounter;

let counter = AtomicCounter::new(3);
assert_eq!(counter.get(), 3);
Source

pub const fn zero() -> Self

Creates a new counter initialized to zero.

§Returns

A counter whose current value is zero.

§Example
use qubit_atomic::AtomicCounter;

let counter = AtomicCounter::zero();
assert!(counter.is_zero());
Source

pub fn get(&self) -> usize

Gets the current counter value.

§Returns

The current counter value.

§Example
use qubit_atomic::AtomicCounter;

let counter = AtomicCounter::new(7);
assert_eq!(counter.get(), 7);
Source

pub fn is_zero(&self) -> bool

Returns whether the current counter value is zero.

§Returns

true if the current value is zero, otherwise false.

§Example
use qubit_atomic::AtomicCounter;

let counter = AtomicCounter::zero();
assert!(counter.is_zero());
Source

pub fn is_positive(&self) -> bool

Returns whether the current counter value is greater than zero.

§Returns

true if the current value is greater than zero, otherwise false.

§Example
use qubit_atomic::AtomicCounter;

let counter = AtomicCounter::new(1);
assert!(counter.is_positive());
Source

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::AtomicCounter;

let counter = AtomicCounter::zero();
assert_eq!(counter.inc(), 1);
Source

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::AtomicCounter;

let counter = AtomicCounter::new(2);
assert_eq!(counter.add(3), 5);
Source

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::AtomicCounter;

let counter = AtomicCounter::new(2);
assert_eq!(counter.try_add(3), Some(5));
Source

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::AtomicCounter;

let counter = AtomicCounter::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.

Source

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::AtomicCounter;

let counter = AtomicCounter::new(1);
assert_eq!(counter.try_dec(), Some(0));
assert_eq!(counter.try_dec(), None);
Source

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::AtomicCounter;

let counter = AtomicCounter::new(5);
assert_eq!(counter.sub(2), 3);
Source

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::AtomicCounter;

let counter = AtomicCounter::new(3);
assert_eq!(counter.try_sub(2), Some(1));
assert_eq!(counter.try_sub(2), None);

Trait Implementations§

Source§

impl Debug for AtomicCounter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AtomicCounter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for AtomicCounter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<usize> for AtomicCounter

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.