pub struct ArcAtomicRef<T> { /* private fields */ }Expand description
Shared-owner wrapper around AtomicRef<T>.
This type is a convenience newtype for Arc<AtomicRef<T>>. Cloning an
ArcAtomicRef clones the shared owner handle, so all clones operate on
the same atomic reference container.
This is different from AtomicRef::clone, which creates a new independent
atomic container that initially points to the same value.
§Example
use qubit_atomic::ArcAtomicRef;
use std::sync::Arc;
let config = ArcAtomicRef::from_value(10);
let shared = config.clone();
shared.store(Arc::new(20));
assert_eq!(*config.load(), 20);
assert_eq!(config.strong_count(), 2);Implementations§
Source§impl<T> ArcAtomicRef<T>
impl<T> ArcAtomicRef<T>
Sourcepub fn from_value(value: T) -> Self
pub fn from_value(value: T) -> Self
Sourcepub fn from_atomic_ref(atomic_ref: AtomicRef<T>) -> Self
pub fn from_atomic_ref(atomic_ref: AtomicRef<T>) -> Self
Wraps an existing AtomicRef<T> in an Arc.
§Parameters
atomic_ref- The atomic reference container to share.
§Returns
A shared atomic reference wrapper owning atomic_ref.
Sourcepub fn strong_count(&self) -> usize
pub fn strong_count(&self) -> usize
Methods from Deref<Target = AtomicRef<T>>§
Sourcepub fn swap(&self, value: Arc<T>) -> Arc<T>
pub fn swap(&self, value: Arc<T>) -> Arc<T>
Swaps the current reference with a new reference, returning the old reference.
§Parameters
value- The new reference to swap in.
§Returns
The old reference.
§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;
let atomic = AtomicRef::new(Arc::new(10));
let old = atomic.swap(Arc::new(20));
assert_eq!(*old, 10);
assert_eq!(*atomic.load(), 20);Sourcepub fn compare_set(&self, current: &Arc<T>, new: Arc<T>) -> Result<(), Arc<T>>
pub fn compare_set(&self, current: &Arc<T>, new: Arc<T>) -> Result<(), Arc<T>>
Compares and sets the reference atomically.
If the current reference equals current (by pointer equality), sets
it to new and returns Ok(()). Otherwise, returns Err(actual)
where actual is the current reference.
§Parameters
current- The expected current reference.new- The new reference to set if current matches.
§Returns
Ok(()) if the pointer comparison succeeds and new is stored.
§Errors
Returns Err(actual) with the observed current reference when the
pointer comparison fails. On failure, new is not installed.
§Note
Comparison uses pointer equality (Arc::ptr_eq), not value equality.
§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;
let atomic = AtomicRef::new(Arc::new(10));
let current = atomic.load();
assert!(atomic.compare_set(¤t, Arc::new(20)).is_ok());
assert_eq!(*atomic.load(), 20);Sourcepub fn compare_set_weak(
&self,
current: &Arc<T>,
new: Arc<T>,
) -> Result<(), Arc<T>>
pub fn compare_set_weak( &self, current: &Arc<T>, new: Arc<T>, ) -> Result<(), Arc<T>>
Weak version of compare-and-set.
This implementation currently delegates to the same lock-free CAS as
compare_set, so it does not introduce extra spurious failures.
§Parameters
current- The expected current reference.new- The new reference to set if current matches.
§Returns
Ok(()) if the pointer comparison succeeds and new is stored.
§Errors
Returns Err(actual) with the observed current reference when the
pointer comparison fails. On failure, new is not installed. This
implementation currently delegates to compare_set
and does not add extra spurious failures.
§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;
let atomic = AtomicRef::new(Arc::new(10));
let mut current = atomic.load();
loop {
match atomic.compare_set_weak(¤t, Arc::new(20)) {
Ok(_) => break,
Err(actual) => current = actual,
}
}
assert_eq!(*atomic.load(), 20);Sourcepub fn compare_and_exchange(&self, current: &Arc<T>, new: Arc<T>) -> Arc<T>
pub fn compare_and_exchange(&self, current: &Arc<T>, new: Arc<T>) -> Arc<T>
Compares and exchanges the reference atomically, returning the previous reference.
If the current reference equals current (by pointer equality), sets
it to new and returns the old reference. Otherwise, returns the
actual current reference.
§Parameters
current- The expected current reference.new- The new reference to set if current matches.
§Returns
The reference before the operation.
§Note
Comparison uses pointer equality (Arc::ptr_eq), not value equality.
§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;
let atomic = AtomicRef::new(Arc::new(10));
let current = atomic.load();
let prev = atomic.compare_and_exchange(¤t, Arc::new(20));
assert!(Arc::ptr_eq(&prev, ¤t));
assert_eq!(*atomic.load(), 20);Sourcepub fn compare_and_exchange_weak(&self, current: &Arc<T>, new: Arc<T>) -> Arc<T>
pub fn compare_and_exchange_weak(&self, current: &Arc<T>, new: Arc<T>) -> Arc<T>
Weak version of compare-and-exchange.
This implementation currently delegates to the same lock-free CAS as
compare_and_exchange, so it does not introduce extra spurious
failures.
§Parameters
current- The expected current reference.new- The new reference to set if current matches.
§Returns
The reference before the operation.
§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;
let atomic = AtomicRef::new(Arc::new(10));
let mut current = atomic.load();
loop {
let prev =
atomic.compare_and_exchange_weak(¤t, Arc::new(20));
if Arc::ptr_eq(&prev, ¤t) {
break;
}
current = prev;
}
assert_eq!(*atomic.load(), 20);Sourcepub fn fetch_update<F>(&self, f: F) -> Arc<T>
pub fn fetch_update<F>(&self, f: F) -> Arc<T>
Updates the reference using a function, returning the old reference.
Internally uses a CAS loop until the update succeeds.
§Parameters
f- A function that takes the current reference and returns the new reference.
§Returns
The old reference before the update.
§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;
let atomic = AtomicRef::new(Arc::new(10));
let old = atomic.fetch_update(|x| Arc::new(**x * 2));
assert_eq!(*old, 10);
assert_eq!(*atomic.load(), 20);