AtomicValue

Struct AtomicValue 

Source
pub struct AtomicValue<T> { /* private fields */ }
Expand description

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§

Source§

impl<T> AtomicValue<T>

Source

pub fn new(value: T) -> Self

Create an AtomicValue with provide value.

§Example
use atomic_value::AtomicValue;

let value = AtomicValue::new(100);
Source

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);
Source

pub fn store(&self, new_value: T)

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);
Source

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);
Source

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§

Source§

impl<T: Debug> Debug for AtomicValue<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Send + Sync> Sync for AtomicValue<T>

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicValue<T>

§

impl<T> !RefUnwindSafe for AtomicValue<T>

§

impl<T> Send for AtomicValue<T>
where T: Sync + Send,

§

impl<T> Unpin for AtomicValue<T>

§

impl<T> UnwindSafe for AtomicValue<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.