Skip to main content

AtomicSignedCount

Struct AtomicSignedCount 

Source
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

Source

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

Creates a new signed atomic counter.

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

A signed counter initialized to value.

§Example
use qubit_atomic::AtomicSignedCount;

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

pub const fn zero() -> Self

Creates a new signed counter initialized to zero.

§Returns

A signed counter whose current value is zero.

§Example
use qubit_atomic::AtomicSignedCount;

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

pub fn get(&self) -> isize

Gets the current counter value.

§Returns

The current counter value.

§Example
use qubit_atomic::AtomicSignedCount;

let counter = AtomicSignedCount::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::AtomicSignedCount;

let counter = AtomicSignedCount::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::AtomicSignedCount;

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

pub fn is_negative(&self) -> bool

Returns whether the current counter value is less than zero.

§Returns

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

§Example
use qubit_atomic::AtomicSignedCount;

let counter = AtomicSignedCount::new(-1);
assert!(counter.is_negative());
Source

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);
Source

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);
Source

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);
Source

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));
Source

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);
Source

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));

Trait Implementations§

Source§

impl Debug for AtomicSignedCount

Source§

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

Formats the current counter value for debugging.

§Parameters
  • f - The formatter receiving the debug representation.
§Returns

A formatting result from the formatter.

Source§

impl Default for AtomicSignedCount

Source§

fn default() -> Self

Creates a zero-valued signed atomic counter.

§Returns

A signed counter whose current value is zero.

Source§

impl Display for AtomicSignedCount

Source§

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

Formats the current counter value with decimal display formatting.

§Parameters
  • f - The formatter receiving the displayed value.
§Returns

A formatting result from the formatter.

Source§

impl From<AtomicSignedCount> for ArcAtomicSignedCount

Source§

fn from(counter: AtomicSignedCount) -> Self

Converts a signed atomic counter into a shared signed counter wrapper.

§Parameters
  • counter - The signed counter container to share.
§Returns

A shared signed counter wrapper owning counter.

Source§

impl From<isize> for AtomicSignedCount

Source§

fn from(value: isize) -> Self

Converts an initial counter value into an AtomicSignedCount.

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

A signed counter initialized to value.

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.