Struct atomic_value::AtomicValue [−][src]
A wrapper provides an atomic load and store of a value.
You can use load
to get the current value which is wrapped by Arc
, or use store
to
set a new value atomically.
Example
use atomic_value::AtomicValue; let value = AtomicValue::new(10); assert_eq!(*value.load(), 10);
Implementations
impl<T> AtomicValue<T>
[src]
pub fn new(value: T) -> Self
[src]
Create an AtomicValue
with provide value.
Example
use atomic_value::AtomicValue; let value = AtomicValue::new(100);
pub fn load(&self) -> Arc<T>
[src]
Load current value atomically.
This function will return a value which wrapped by an Arc
.
After get the value, it won’t be effect by store
, swap
or compare_and_swap
.
Example
use atomic_value::AtomicValue; let value = AtomicValue::new(10); assert_eq!(*value.load(), 10);
pub fn store(&self, new_value: T)
[src]
Store a new value atomically.
The new value won’t change any loaded value. This method like swap
but won’t return
old value.
Example
use atomic_value::AtomicValue; let value = AtomicValue::new(10); let old = value.load(); assert_eq!(*old, 10); value.store(100); assert_eq!(*value.load(), 100);
pub fn swap(&self, new_value: T) -> Arc<T>
[src]
Store a new value into AtomicValue
, and return the old value with Arc
wrapping.
The new value won’t change any loaded value. This method like store
but will return
old value.
Example
use atomic_value::AtomicValue; let value = AtomicValue::new(10); let old = value.load(); assert_eq!(*old, 10); let old = value.swap(100); assert_eq!(*old ,10); assert_eq!(*value.load(), 100);
pub fn compare_exchange(
&self,
current: &T,
new_value: T,
success: Ordering,
failure: Ordering
) -> Result<Arc<T>, Error<T>> where
T: PartialEq,
[src]
&self,
current: &T,
new_value: T,
success: Ordering,
failure: Ordering
) -> Result<Arc<T>, Error<T>> where
T: PartialEq,
Stores a value into the AtomicValue if the current value is the same as the current
value.
The new value won’t change any loaded values.
Example
use atomic_value::{AtomicValue, Error}; use std::sync::atomic::Ordering; let value = AtomicValue::new(10); let result = value.compare_exchange(&10, 20, Ordering::AcqRel, Ordering::Acquire); assert_eq!(*result.unwrap(), 10); let result = value.compare_exchange(&10, 30, Ordering::AcqRel, Ordering::Acquire); let Error(current, new_value) = result.unwrap_err(); assert_eq!(*current, 20); assert_eq!(new_value, 30);
Trait Implementations
impl<T: Debug> Debug for AtomicValue<T>
[src]
impl<T: Send + Sync> Sync for AtomicValue<T>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for AtomicValue<T>
impl<T> Send for AtomicValue<T> where
T: Send + Sync,
T: Send + Sync,
impl<T> Unpin for AtomicValue<T>
impl<T> UnwindSafe for AtomicValue<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,