Struct AtomicRef

Source
#[repr(C)]
pub struct AtomicRef<'a, T: 'a> { /* private fields */ }
Expand description

A mutable Option<&'a T> type which can be safely shared between threads.

Implementations§

Source§

impl<'a, T> AtomicRef<'a, T>

Source

pub const fn new(p: Option<&'a T>) -> AtomicRef<'a, T>

Creates a new AtomicRef.

§Examples
use atomic_ref::AtomicRef;

static VALUE: i32 = 10;
let atomic_ref = AtomicRef::new(Some(&VALUE));
Source

pub fn get_mut(&mut self) -> &mut Option<&'a T>

Returns a mutable reference to the underlying Option<&'a T>.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

§Examples
use std::sync::atomic::Ordering;
use atomic_ref::AtomicRef;

let value: i32 = 10;
let value2: i32 = 20;

let mut some_ref = AtomicRef::new(Some(&value));
assert_eq!(*some_ref.get_mut(), Some(&value));
*some_ref.get_mut() = Some(&value2);
assert_eq!(some_ref.load(Ordering::SeqCst), Some(&value2));
Source

pub fn into_inner(self) -> Option<&'a T>

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

§Examples
use atomic_ref::AtomicRef;

let some_ref = AtomicRef::new(Some(&5));
assert_eq!(some_ref.into_inner(), Some(&5));
Source

pub fn load(&self, ordering: Ordering) -> Option<&'a T>

Loads the value stored in the AtomicRef.

load takes an Ordering argument which describes the memory ordering of this operation.

Calls with ordering weaker than Acquire will be performed with Acquire ordering.

§Panics

Panics if order is Release or AcqRel.

§Examples
use std::sync::atomic::Ordering;
use atomic_ref::AtomicRef;

static VALUE: i32 = 10;

let some_ref = AtomicRef::new(Some(&VALUE));
assert_eq!(some_ref.load(Ordering::Acquire), Some(&10));
Source

pub fn store(&self, ptr: Option<&'a T>, order: Ordering)

Stores a value into the AtomicRef.

store takes an Ordering argument which describes the memory ordering of this operation.

Calls with ordering weaker than Release will be performed with Release ordering.

§Panics

Panics if order is Acquire or AcqRel.

§Examples
use std::sync::atomic::Ordering;
use atomic_ref::AtomicRef;

static VALUE: i32 = 10;

let some_ptr = AtomicRef::new(None);
some_ptr.store(Some(&VALUE), Ordering::SeqCst);
Source

pub fn swap(&self, p: Option<&'a T>, order: Ordering) -> Option<&'a T>

Stores a value into the AtomicRef, returning the old value.

swap takes an Ordering argument which describes the memory ordering of this operation.

Calls with ordering weaker than AcqRel will be performed with AcqRel ordering.

§Examples
use std::sync::atomic::Ordering;
use atomic_ref::AtomicRef;

static VALUE: i32 = 10;
static OTHER_VALUE: i32 = 20;

let some_ptr = AtomicRef::new(Some(&VALUE));
let value = some_ptr.swap(Some(&OTHER_VALUE), Ordering::SeqCst);
Source

pub fn compare_and_swap( &self, current: Option<&'a T>, new: Option<&'a T>, order: Ordering, ) -> Option<&'a T>

Stores a value into the AtomicRef if the current value is the “same” as the current value.

The return value is always the previous value. If it the “same” as current, then the value was updated.

This method considers two Option<&'a T>s to be the “same” if they are both Some and have the same pointer value, or if they are both None. This method does not use Eq or PartialEq for comparison.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation.

Calls with ordering weaker than AcqRel will be performed with AcqRel ordering.

§Examples
use std::sync::atomic::Ordering;
use atomic_ref::AtomicRef;

static VALUE: i32 = 10;
static OTHER_VALUE: i32 = 20;

let some_ptr = AtomicRef::new(Some(&VALUE));
let value = some_ptr.compare_and_swap(Some(&OTHER_VALUE), None, Ordering::SeqCst);
Source

pub fn compare_exchange( &self, current: Option<&'a T>, new: Option<&'a T>, success: Ordering, failure: Ordering, ) -> Result<Option<&'a T>, Option<&'a T>>

Stores a value into the AtomicRef if the current value is the “same” as the current value.

The return value is a result indicating whether the new value was written, and containing the previous value. On success this value is guaranteed to be the “same” as new.

This method considers two Option<&'a T>s to be the “same” if they are both Some and have the same pointer value, or if they are both None. This method does not use Eq or PartialEq for comparison.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering if the operation succeeds while the second describes the required ordering when the operation fails. The failure ordering can’t be Release or AcqRel and must be equivalent or weaker than the success ordering.

Calls with a success ordering weaker than AcqRel or failure ordering weaker than Acquire will be performed with those orderings.

§Examples
use std::sync::atomic::Ordering;
use atomic_ref::AtomicRef;

static VALUE: i32 = 10;
static OTHER_VALUE: i32 = 20;

let some_ptr = AtomicRef::new(Some(&VALUE));
let value = some_ptr.compare_exchange(Some(&OTHER_VALUE), None,
                                      Ordering::SeqCst, Ordering::Acquire);
Source

pub fn compare_exchange_weak( &self, current: Option<&'a T>, new: Option<&'a T>, success: Ordering, failure: Ordering, ) -> Result<Option<&'a T>, Option<&'a T>>

Stores a value into the pointer if the current value is the same as the current value.

Unlike compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering if the operation succeeds while the second describes the required ordering when the operation fails. The failure ordering can’t be Release or AcqRel and must be equivalent or weaker than the success ordering.

Calls with a success ordering weaker than AcqRel or failure ordering weaker than Acquire will be performed with those orderings.

§Examples
use std::sync::atomic::Ordering;
use atomic_ref::AtomicRef;

static VALUE: i32 = 10;
static OTHER_VALUE: i32 = 20;

let some_ptr = AtomicRef::new(Some(&VALUE));

let mut old = some_ptr.load(Ordering::Acquire);
loop {
    match some_ptr.compare_exchange_weak(old, Some(&VALUE),
                                         Ordering::SeqCst, Ordering::Acquire) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}

Trait Implementations§

Source§

impl<'a, T: Debug> Debug for AtomicRef<'a, T>

Source§

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

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

impl<'a, T> Default for AtomicRef<'a, T>

Source§

fn default() -> AtomicRef<'a, T>

Returns the “default value” for a type. Read more
Source§

impl<'a, T> From<Option<&'a T>> for AtomicRef<'a, T>

Source§

fn from(other: Option<&'a T>) -> AtomicRef<'a, T>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, T> !Freeze for AtomicRef<'a, T>

§

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

§

impl<'a, T> Send for AtomicRef<'a, T>
where T: Sync,

§

impl<'a, T> Sync for AtomicRef<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for AtomicRef<'a, T>

§

impl<'a, T> !UnwindSafe for AtomicRef<'a, T>

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