Skip to main content

ArcAtomicCount

Struct ArcAtomicCount 

Source
pub struct ArcAtomicCount { /* private fields */ }
Expand description

Shared-owner wrapper around AtomicCount.

This type is a convenience newtype for Arc<AtomicCount>. Cloning an ArcAtomicCount clones the shared owner handle, so all clones operate on the same underlying non-negative counter.

§Example

use qubit_atomic::ArcAtomicCount;

let counter = ArcAtomicCount::zero();
let shared = counter.clone();

shared.inc();
assert_eq!(counter.get(), 1);
assert_eq!(counter.strong_count(), 2);

Implementations§

Source§

impl ArcAtomicCount

Source

pub fn new(value: usize) -> Self

Creates a new shared non-negative atomic counter.

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

A shared counter wrapper initialized to value.

Source

pub fn zero() -> Self

Creates a new shared counter initialized to zero.

§Returns

A shared counter wrapper whose current value is zero.

Source

pub fn from_count(counter: AtomicCount) -> Self

Wraps an existing AtomicCount in an Arc.

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

A shared counter wrapper owning counter.

Source

pub fn from_arc(inner: Arc<AtomicCount>) -> Self

Wraps an existing shared atomic counter.

§Parameters
  • inner - The shared atomic counter to wrap.
§Returns

A wrapper around inner.

Source

pub fn as_arc(&self) -> &Arc<AtomicCount>

Returns the underlying Arc without cloning it.

§Returns

A shared reference to the underlying Arc<AtomicCount>.

Source

pub fn into_arc(self) -> Arc<AtomicCount>

Consumes this wrapper and returns the underlying Arc.

§Returns

The underlying Arc<AtomicCount>.

Source

pub fn strong_count(&self) -> usize

Returns the number of strong Arc owners.

§Returns

The current strong reference count of the shared counter.

Methods from Deref<Target = AtomicCount>§

Source

pub fn get(&self) -> usize

Gets the current counter value.

§Returns

The current counter value.

§Example
use qubit_atomic::AtomicCount;

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

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

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

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

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

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

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

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

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

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

Trait Implementations§

Source§

impl Clone for ArcAtomicCount

Source§

fn clone(&self) -> Self

Clones the shared owner handle.

§Returns

A new wrapper pointing to the same underlying atomic counter.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ArcAtomicCount

Source§

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

Formats the current counter value and sharing state for debugging.

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

A formatting result from the formatter.

Source§

impl Default for ArcAtomicCount

Source§

fn default() -> Self

Creates a zero-valued shared atomic counter.

§Returns

A shared counter wrapper whose current value is zero.

Source§

impl Deref for ArcAtomicCount

Source§

fn deref(&self) -> &Self::Target

Dereferences to the underlying AtomicCount.

§Returns

A shared reference to the atomic counter.

Source§

type Target = AtomicCount

The resulting type after dereferencing.
Source§

impl Display for ArcAtomicCount

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<Arc<AtomicCount>> for ArcAtomicCount

Source§

fn from(inner: Arc<AtomicCount>) -> Self

Converts an existing shared atomic counter into its wrapper.

§Parameters
  • inner - The shared atomic counter to wrap.
§Returns

A wrapper around inner.

Source§

impl From<AtomicCount> for ArcAtomicCount

Source§

fn from(counter: AtomicCount) -> Self

Converts an atomic counter into a shared atomic counter wrapper.

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

A shared counter wrapper owning counter.

Source§

impl From<usize> for ArcAtomicCount

Source§

fn from(value: usize) -> Self

Converts an initial counter value into a shared atomic counter.

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

A shared counter wrapper 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.