pub struct ArcAtomicSignedCount { /* private fields */ }Expand description
Shared-owner wrapper around AtomicSignedCount.
This type is a convenience newtype for Arc<AtomicSignedCount>. Cloning an
ArcAtomicSignedCount clones the shared owner handle, so all clones
operate on the same underlying signed counter.
§Example
use qubit_atomic::ArcAtomicSignedCount;
let counter = ArcAtomicSignedCount::zero();
let shared = counter.clone();
shared.sub(3);
assert_eq!(counter.get(), -3);
assert_eq!(counter.strong_count(), 2);Implementations§
Source§impl ArcAtomicSignedCount
impl ArcAtomicSignedCount
Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates a new shared signed counter initialized to zero.
§Returns
A shared signed counter wrapper whose current value is zero.
Sourcepub fn from_count(counter: AtomicSignedCount) -> Self
pub fn from_count(counter: AtomicSignedCount) -> Self
Wraps an existing AtomicSignedCount in an Arc.
§Parameters
counter- The signed counter container to share.
§Returns
A shared signed counter wrapper owning counter.
Sourcepub fn from_arc(inner: Arc<AtomicSignedCount>) -> Self
pub fn from_arc(inner: Arc<AtomicSignedCount>) -> Self
Sourcepub fn as_arc(&self) -> &Arc<AtomicSignedCount>
pub fn as_arc(&self) -> &Arc<AtomicSignedCount>
Sourcepub fn into_arc(self) -> Arc<AtomicSignedCount>
pub fn into_arc(self) -> Arc<AtomicSignedCount>
Sourcepub fn strong_count(&self) -> usize
pub fn strong_count(&self) -> usize
Methods from Deref<Target = 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));Trait Implementations§
Source§impl Clone for ArcAtomicSignedCount
impl Clone for ArcAtomicSignedCount
Source§impl Debug for ArcAtomicSignedCount
impl Debug for ArcAtomicSignedCount
Source§impl Default for ArcAtomicSignedCount
impl Default for ArcAtomicSignedCount
Source§impl Deref for ArcAtomicSignedCount
impl Deref for ArcAtomicSignedCount
Source§fn deref(&self) -> &Self::Target
fn deref(&self) -> &Self::Target
Dereferences to the underlying AtomicSignedCount.
§Returns
A shared reference to the signed atomic counter.