Skip to main content

AtomicRef

Struct AtomicRef 

Source
pub struct AtomicRef<T> { /* private fields */ }
Expand description

Atomic reference type.

Provides easy-to-use atomic operations on references with automatic memory ordering selection. Uses Arc<T> for thread-safe reference counting.

§Implementation Details

This type is backed by arc_swap::ArcSwap<T>, which provides lock-free, memory-safe atomic replacement and loading of Arc<T> values without exposing raw-pointer lifetime hazards.

§Features

  • Automatic memory ordering selection
  • Thread-safe reference counting via Arc
  • Functional update operations
  • Zero-cost abstraction with inline methods

§Example

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

#[derive(Debug, Clone)]
struct Config {
    timeout: u64,
    max_retries: u32,
}

let config = Arc::new(Config {
    timeout: 1000,
    max_retries: 3,
});

let atomic_config = AtomicRef::new(config);

// Update configuration
let new_config = Arc::new(Config {
    timeout: 2000,
    max_retries: 5,
});

let old_config = atomic_config.swap(new_config);
assert_eq!(old_config.timeout, 1000);
assert_eq!(atomic_config.load().timeout, 2000);

§Author

Haixing Hu

Implementations§

Source§

impl<T> AtomicRef<T>

Source

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

Creates a new atomic reference.

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

let data = Arc::new(42);
let atomic = AtomicRef::new(data);
assert_eq!(*atomic.load(), 42);
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 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(()) on success, or Err(actual) on failure.

§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(()) on success, or Err(actual) on failure.

§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.

§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 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> Atomic for AtomicRef<T>

Source§

type Value = Arc<T>

The value type stored in the atomic.
Source§

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

Loads the current value. Read more
Source§

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

Stores a new value. Read more
Source§

fn swap(&self, value: Arc<T>) -> Arc<T>

Swaps the current value with a new value, returning the old value. Read more
Source§

fn compare_set(&self, current: Arc<T>, new: Arc<T>) -> Result<(), Arc<T>>

Compares and sets the value atomically. Read more
Source§

fn compare_set_weak(&self, current: Arc<T>, new: Arc<T>) -> Result<(), Arc<T>>

Weak version of compare-and-set. Read more
Source§

fn compare_exchange(&self, current: Arc<T>, new: Arc<T>) -> Arc<T>

Compares and exchanges the value atomically, returning the previous value. Read more
Source§

fn compare_exchange_weak(&self, current: Arc<T>, new: Arc<T>) -> Arc<T>

Weak version of compare-and-exchange. Read more
Source§

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

Updates the value using a function, returning the old value. Read more
Source§

impl<T> Clone for AtomicRef<T>

Source§

fn clone(&self) -> Self

Clones the atomic reference.

Creates a new AtomicRef that initially points to the same value as the original, but subsequent atomic operations are independent.

1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for AtomicRef<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Display> Display for AtomicRef<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicRef<T>

§

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

§

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

§

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

§

impl<T> Unpin for AtomicRef<T>

§

impl<T> UnsafeUnpin for AtomicRef<T>

§

impl<T> UnwindSafe for AtomicRef<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<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.