[−][src]Struct atomic_value::AtomicValue
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);
Methods
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_and_swap(&self, current: &T, new_value: T) -> Arc<T> where
T: PartialEq,
[src]
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 value. This method like swap
, will return the
old value no matter the current value is the same as the current
value.
Example
use atomic_value::AtomicValue; let value = AtomicValue::new(10); assert_eq!(*value.compare_and_swap(&10, 20), 10); assert_eq!(*value.compare_and_swap(&10, 30), 20)
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,
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.
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>,