Struct AtomicOption

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

An atomic version of Option<Box<T>>, useful for moving owned objects between threads in a wait-free manner.

Implementations§

Source§

impl<T> AtomicOption<T>

Source

pub fn new(data: Box<T>) -> AtomicOption<T>

Create a new AtomicOption storing the specified data.

let opt = AtomicOption::new(Box::new(7));
let value = opt.take(Ordering::SeqCst).unwrap();
assert_eq!(value, Box::new(7));
Source

pub unsafe fn from_raw(ptr: *mut T) -> AtomicOption<T>

Create a new AtomicOption from a raw pointer.

Source

pub fn empty() -> AtomicOption<T>

Create a new AtomicOption storing None.

let opt: AtomicOption<()> = AtomicOption::empty();
let value = opt.take(Ordering::SeqCst);
assert!(value.is_none());
Source

pub fn take(&self, ordering: Ordering) -> Option<Box<T>>

Take the value out of the AtomicOption, if there is one.

let opt = AtomicOption::new(Box::new(178));
let first_take = opt.take(Ordering::SeqCst);
let second_take = opt.take(Ordering::SeqCst);

assert_eq!(first_take, Some(Box::new(178)));
assert!(second_take.is_none());
Source

pub fn swap(&self, new: Box<T>, ordering: Ordering) -> Option<Box<T>>

Swap the value in the AtomicOption with a new one, returning the old value if there was one.

let opt = AtomicOption::new(Box::new(1236));
let old = opt.swap(Box::new(542), Ordering::SeqCst).unwrap();
assert_eq!(old, Box::new(1236));

let new = opt.take(Ordering::SeqCst).unwrap();
assert_eq!(new, Box::new(542));
Source

pub fn replace(&self, new: Option<Box<T>>, ordering: Ordering) -> Option<Box<T>>

Replace the Option in the AtomicOption with a new one, returning the old option.

let opt = AtomicOption::empty();
let old = opt.replace(Some(Box::new("hello")), Ordering::SeqCst);
assert!(old.is_none());

let new = opt.take(Ordering::SeqCst).unwrap();
assert_eq!(new, Box::new("hello"));
Source

pub fn try_store(&self, new: Box<T>, ordering: Ordering) -> Option<Box<T>>

Store the new value in the AtomicOption iff it currently contains a None.

None is returned if the store succeeded, or Some is returned with the rejected data if the store fails.

This operation is implemented as a single atomic compare_and_swap.

let opt = AtomicOption::empty();
let stored = opt.try_store(Box::new("some data"), Ordering::SeqCst);
assert!(stored.is_none());

let stored2 = opt.try_store(Box::new("some more data"), Ordering::SeqCst);
assert_eq!(stored2, Some(Box::new("some more data")));

let value = opt.take(Ordering::SeqCst).unwrap();
assert_eq!(value, Box::new("some data"));
Source

pub fn spinlock(&self, ordering: Ordering) -> Box<T>

Execute a compare_and_swap loop until there is a value in the AtomicOption, then return it.


// We'll use an AtomicOption as a lightweight channel transferring data via a spinlock.
let tx = Arc::new(AtomicOption::empty());
let rx = tx.clone();

thread::spawn(move || {
    assert_eq!(*rx.spinlock(Ordering::Acquire), 7);
});

tx.swap(Box::new(7), Ordering::Release);
Source

pub fn load_raw(&self, ordering: Ordering) -> *const T

Get the raw value stored in the AtomicOption.

§Safety

It is almost never safe to read from this pointer.

Trait Implementations§

Source§

impl<T> Drop for AtomicOption<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<Option<Box<T>>> for AtomicOption<T>

Source§

fn from(opt: Option<Box<T>>) -> AtomicOption<T>

Converts to this type from the input type.
Source§

impl<T> Sync for AtomicOption<T>

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicOption<T>

§

impl<T> RefUnwindSafe for AtomicOption<T>
where T: RefUnwindSafe,

§

impl<T> Send for AtomicOption<T>
where T: Send,

§

impl<T> Unpin for AtomicOption<T>

§

impl<T> UnwindSafe for AtomicOption<T>
where T: UnwindSafe,

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.