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§
Required Methods§
Sourcefn get_mut(&mut self) -> &mut Self::Type
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);Sourcefn into_inner(self) -> Self::Type
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);Sourcefn load(&self, order: Ordering) -> Self::Type
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);Sourcefn store(&self, val: Self::Type, order: Ordering)
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);Sourcefn swap(&self, val: Self::Type, order: Ordering) -> Self::Type
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);Sourcefn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
👎Deprecated: Use compare_exchange or compare_exchange_weak instead
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadStores 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);Sourcefn compare_exchange(
&self,
current: Self::Type,
new: Self::Type,
success: Ordering,
failure: Ordering,
) -> Result<Self::Type, Self::Type>
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);Sourcefn compare_exchange_weak(
&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>
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
impl Atomic for AtomicBool
type Type = bool
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicI8
impl Atomic for AtomicI8
type Type = i8
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicI16
impl Atomic for AtomicI16
type Type = i16
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicI32
impl Atomic for AtomicI32
type Type = i32
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicI64
impl Atomic for AtomicI64
type Type = i64
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicIsize
impl Atomic for AtomicIsize
type Type = isize
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicU8
impl Atomic for AtomicU8
type Type = u8
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicU16
impl Atomic for AtomicU16
type Type = u16
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicU32
impl Atomic for AtomicU32
type Type = u32
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicU64
impl Atomic for AtomicU64
type Type = u64
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl Atomic for AtomicUsize
impl Atomic for AtomicUsize
type Type = usize
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak insteadfn 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>
Source§impl<T> Atomic for AtomicPtr<T>
impl<T> Atomic for AtomicPtr<T>
type Type = *mut T
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
Source§fn compare_and_swap(
&self,
current: Self::Type,
new: Self::Type,
order: Ordering,
) -> Self::Type
fn compare_and_swap( &self, current: Self::Type, new: Self::Type, order: Ordering, ) -> Self::Type
compare_exchange or compare_exchange_weak instead