Skip to main content

ArcAtomicRef

Struct ArcAtomicRef 

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

Source

pub fn new(value: Arc<T>) -> Self

Creates a new shared atomic reference from an Arc<T>.

§Parameters
  • value - The initial shared value stored in the atomic reference.
§Returns

A shared atomic reference wrapper initialized to value.

Source

pub fn from_value(value: T) -> Self

Creates a new shared atomic reference from an owned value.

§Parameters
  • value - The owned value to store as the initial reference.
§Returns

A shared atomic reference wrapper initialized to Arc::new(value).

Source

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.

Source

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

Wraps an existing shared atomic reference container.

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

A wrapper around inner.

Source

pub fn as_arc(&self) -> &Arc<AtomicRef<T>>

Returns the underlying Arc without cloning it.

§Returns

A shared reference to the underlying Arc<AtomicRef<T>>.

Source

pub fn into_arc(self) -> Arc<AtomicRef<T>>

Consumes this wrapper and returns the underlying Arc.

§Returns

The underlying Arc<AtomicRef<T>>.

Source

pub fn strong_count(&self) -> usize

Returns the number of strong Arc owners.

§Returns

The current strong reference count of the shared atomic reference container.

Methods from Deref<Target = AtomicRef<T>>§

Source

pub fn load(&self) -> Arc<T>

Gets the current reference.

§Returns

A cloned Arc pointing to the current value.

§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;

let atomic = AtomicRef::new(Arc::new(42));
let value = atomic.load();
assert_eq!(*value, 42);
Source

pub fn load_guard(&self) -> Guard<Arc<T>>

Gets the current reference as an ArcSwap guard.

This is useful for short-lived reads because it avoids cloning the underlying Arc on the fast path. Use load when the caller needs an owned Arc<T> that can be stored or moved freely.

§Returns

A guard pointing to the current Arc.

§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;

let atomic = AtomicRef::new(Arc::new(42));
let guard = atomic.load_guard();
assert_eq!(**guard, 42);
Source

pub fn store(&self, value: Arc<T>)

Sets a new reference.

§Parameters
  • value - The new reference to set.
§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;

let atomic = AtomicRef::new(Arc::new(42));
atomic.store(Arc::new(100));
assert_eq!(*atomic.load(), 100);
Source

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

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(&current, Arc::new(20)).is_ok());
assert_eq!(*atomic.load(), 20);
Source

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(&current, Arc::new(20)) {
        Ok(_) => break,
        Err(actual) => current = actual,
    }
}
assert_eq!(*atomic.load(), 20);
Source

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(&current, Arc::new(20));
assert!(Arc::ptr_eq(&prev, &current));
assert_eq!(*atomic.load(), 20);
Source

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(&current, Arc::new(20));
    if Arc::ptr_eq(&prev, &current) {
        break;
    }
    current = prev;
}
assert_eq!(*atomic.load(), 20);
Source

pub fn fetch_update<F>(&self, f: F) -> Arc<T>
where F: Fn(&Arc<T>) -> 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.

The closure may be called more than once when concurrent updates cause CAS retries.

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

pub fn try_update<F>(&self, f: F) -> Option<Arc<T>>
where F: Fn(&Arc<T>) -> Option<Arc<T>>,

Conditionally updates the reference using a function.

Internally uses a pointer-based CAS loop until the update succeeds or the closure rejects the current reference by returning None.

§Parameters
  • f - A function that takes the current reference and returns the new reference, or None to leave the current reference unchanged.
§Returns

Some(old_reference) when the update succeeds, or None when f rejects the observed current reference.

The closure may be called more than once when concurrent updates cause CAS retries.

§Example
use qubit_atomic::AtomicRef;
use std::sync::Arc;

let atomic = AtomicRef::new(Arc::new(3));
let old = atomic.try_update(|current| {
    (**current % 2 == 1).then_some(Arc::new(**current + 1))
});
assert_eq!(*old.unwrap(), 3);
assert_eq!(*atomic.load(), 4);
assert!(atomic
    .try_update(|current| {
        (**current % 2 == 1).then_some(Arc::new(**current + 1))
    })
    .is_none());
assert_eq!(*atomic.load(), 4);
Source

pub fn inner(&self) -> &ArcSwap<T>

Gets a reference to the underlying ArcSwap.

This allows advanced users to access lower-level ArcSwap APIs for custom synchronization strategies.

§Returns

A reference to the underlying arc_swap::ArcSwap<T>.

Trait Implementations§

Source§

impl<T> Clone for ArcAtomicRef<T>

Source§

fn clone(&self) -> Self

Clones the shared owner handle.

§Returns

A new wrapper pointing to the same underlying atomic reference container.

1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ArcAtomicRef<T>

Source§

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

Formats the currently loaded reference and sharing state for debugging.

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

A formatting result from the formatter.

Source§

impl<T> Deref for ArcAtomicRef<T>

Source§

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

Dereferences to the underlying AtomicRef<T>.

§Returns

A shared reference to the atomic reference container.

Source§

type Target = AtomicRef<T>

The resulting type after dereferencing.
Source§

impl<T: Display> Display for ArcAtomicRef<T>

Source§

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

Formats the currently loaded reference with display formatting.

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

A formatting result from the formatter.

Source§

impl<T> From<Arc<AtomicRef<T>>> for ArcAtomicRef<T>

Source§

fn from(inner: Arc<AtomicRef<T>>) -> Self

Converts an existing shared atomic reference container into its wrapper.

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

A wrapper around inner.

Source§

impl<T> From<AtomicRef<T>> for ArcAtomicRef<T>

Source§

fn from(atomic_ref: AtomicRef<T>) -> Self

Converts an atomic reference container into a shared wrapper.

§Parameters
  • atomic_ref - The atomic reference container to share.
§Returns

A shared atomic reference wrapper owning atomic_ref.

Auto Trait Implementations§

§

impl<T> Freeze for ArcAtomicRef<T>

§

impl<T> RefUnwindSafe for ArcAtomicRef<T>
where T: RefUnwindSafe,

§

impl<T> Send for ArcAtomicRef<T>
where T: Sync + Send,

§

impl<T> Sync for ArcAtomicRef<T>
where T: Sync + Send,

§

impl<T> Unpin for ArcAtomicRef<T>

§

impl<T> UnsafeUnpin for ArcAtomicRef<T>

§

impl<T> UnwindSafe for ArcAtomicRef<T>
where T: RefUnwindSafe,

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.