Atomic

Trait 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,
    }
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Atomic for AtomicBool

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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>

Source§

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§