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
impl ArcAtomicCount
Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates a new shared counter initialized to zero.
§Returns
A shared counter wrapper whose current value is zero.
Sourcepub fn from_count(counter: AtomicCount) -> Self
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.
Sourcepub fn from_arc(inner: Arc<AtomicCount>) -> Self
pub fn from_arc(inner: Arc<AtomicCount>) -> Self
Sourcepub fn as_arc(&self) -> &Arc<AtomicCount>
pub fn as_arc(&self) -> &Arc<AtomicCount>
Sourcepub fn into_arc(self) -> Arc<AtomicCount>
pub fn into_arc(self) -> Arc<AtomicCount>
Sourcepub fn strong_count(&self) -> usize
pub fn strong_count(&self) -> usize
Methods from Deref<Target = 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);