pub struct AtomicValue<T> { /* private fields */ }Expand description
Implementations§
Source§impl<T> AtomicValue<T>
impl<T> AtomicValue<T>
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Create an AtomicValue with provide value.
§Example
use atomic_value::AtomicValue;
let value = AtomicValue::new(100);Sourcepub fn load(&self) -> Arc<T>
pub fn load(&self) -> Arc<T>
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);Sourcepub fn swap(&self, new_value: T) -> Arc<T>
pub fn swap(&self, new_value: T) -> Arc<T>
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);Sourcepub fn compare_exchange(
&self,
current: &T,
new_value: T,
success: Ordering,
failure: Ordering,
) -> Result<Arc<T>, Error<T>>where
T: PartialEq,
pub fn compare_exchange(
&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§
Auto Trait Implementations§
impl<T> !Freeze for AtomicValue<T>
impl<T> !RefUnwindSafe for AtomicValue<T>
impl<T> Send for AtomicValue<T>
impl<T> Unpin for AtomicValue<T>
impl<T> UnwindSafe for AtomicValue<T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more