Trait atomic_traits::Atomic

source ·
pub trait Atomic {
    type Type;

    // Required methods
    fn new(v: Self::Type) -> Self;
    fn get_mut(&mut self) -> &mut Self::Type;
    fn into_inner(self) -> Self::Type;
    fn load(&self, order: Ordering) -> Self::Type;
    fn store(&self, val: Self::Type, order: Ordering);
    fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type;
    fn compare_and_swap(
        &self,
        current: Self::Type,
        new: Self::Type,
        order: Ordering
    ) -> Self::Type;
    fn compare_exchange(
        &self,
        current: Self::Type,
        new: Self::Type,
        success: Ordering,
        failure: Ordering
    ) -> Result<Self::Type, Self::Type>;
    fn compare_exchange_weak(
        &self,
        current: Self::Type,
        new: Self::Type,
        success: Ordering,
        failure: Ordering
    ) -> Result<Self::Type, Self::Type>;
}
Expand description

Generic atomic types

Required Associated Types§

source

type Type

The underlying type

Required Methods§

source

fn new(v: Self::Type) -> Self

Creates a new atomic type.

source

fn get_mut(&mut self) -> &mut Self::Type

Returns a mutable reference to the underlying type.

§Examples
use std::sync::atomic::{AtomicBool, Ordering};
use atomic_traits::Atomic;

let mut some_bool = AtomicBool::new(true);
assert_eq!(*Atomic::get_mut(&mut some_bool), true);
*Atomic::get_mut(&mut some_bool) = false;
assert_eq!(Atomic::load(&some_bool, Ordering::SeqCst), false);
source

fn into_inner(self) -> Self::Type

Consumes the atomic and returns the contained value.

§Examples
use std::sync::atomic::AtomicBool;
use atomic_traits::Atomic;

let some_bool = AtomicBool::new(true);
assert_eq!(Atomic::into_inner(some_bool), true);
source

fn load(&self, order: Ordering) -> Self::Type

Loads a value from the atomic type.

§Examples
use std::sync::atomic::{AtomicBool, Ordering};
use atomic_traits::Atomic;

let some_bool = AtomicBool::new(true);

assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), true);
source

fn store(&self, val: Self::Type, order: Ordering)

Stores a value into the atomic type.

§Examples
use std::sync::atomic::{AtomicBool, Ordering};
use atomic_traits::Atomic;

let some_bool = AtomicBool::new(true);

Atomic::store(&some_bool, false, Ordering::Relaxed);
assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
source

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

Stores a value into the atomic type, returning the previous value.

§Examples
use std::sync::atomic::{AtomicBool, Ordering};
use atomic_traits::Atomic;

let some_bool = AtomicBool::new(true);

assert_eq!(Atomic::swap(&some_bool, false, Ordering::Relaxed), true);
assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
source

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic type if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

§Examples
use std::sync::atomic::{AtomicBool, Ordering};
use atomic_traits::Atomic;

let some_bool = AtomicBool::new(true);

assert_eq!(Atomic::compare_and_swap(&some_bool, true, false, Ordering::Relaxed), true);
assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);

assert_eq!(Atomic::compare_and_swap(&some_bool, true, true, Ordering::Relaxed), false);
assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
source

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

Stores a value into the atomic type 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 equal to current.

§Examples
use std::sync::atomic::{AtomicBool, Ordering};
use atomic_traits::Atomic;

let some_bool = AtomicBool::new(true);

assert_eq!(Atomic::compare_exchange(&some_bool,
                                     true,
                                     false,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(true));
assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);

assert_eq!(Atomic::compare_exchange(&some_bool,
                                    true,
                                    true,
                                    Ordering::SeqCst,
                                    Ordering::Acquire),
           Err(false));
assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
source

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

Stores a value into the atomic type 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.

§Examples
use std::sync::atomic::{AtomicBool, Ordering};
use atomic_traits::Atomic;

let val = AtomicBool::new(false);

let new = true;
let mut old = Atomic::load(&val, Ordering::Relaxed);
loop {
    match Atomic::compare_exchange_weak(&val, old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Atomic for AtomicBool

§

type Type = bool

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicI8

§

type Type = i8

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicI16

§

type Type = i16

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicI32

§

type Type = i32

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicI64

§

type Type = i64

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicIsize

§

type Type = isize

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicU8

§

type Type = u8

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicU16

§

type Type = u16

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicU32

§

type Type = u32

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicU64

§

type Type = u64

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl Atomic for AtomicUsize

§

type Type = usize

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

impl<T> Atomic for AtomicPtr<T>

§

type Type = *mut T

source§

fn new(v: Self::Type) -> Self

source§

fn get_mut(&mut self) -> &mut Self::Type

source§

fn into_inner(self) -> Self::Type

source§

fn load(&self, order: Ordering) -> Self::Type

source§

fn store(&self, val: Self::Type, order: Ordering)

source§

fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type

source§

fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering ) -> Self::Type

👎Deprecated: Use compare_exchange or compare_exchange_weak instead
source§

fn compare_exchange( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

source§

fn compare_exchange_weak( &self, current: Self::Type, new: Self::Type, success: Ordering, failure: Ordering ) -> Result<Self::Type, Self::Type>

Implementors§