Struct AtomicOptionBox

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

A type that holds a single Option<Box<T>> value and can be safely shared between threads.

Implementations§

Source§

impl<T> AtomicOptionBox<T>

Source

pub fn new(value: Option<Box<T>>) -> AtomicOptionBox<T>

Creates a new AtomicOptionBox with the given value.

§Examples
use mea::atomicbox::AtomicOptionBox;

let atomic_box = AtomicOptionBox::new(Some(Box::new(0)));
Source

pub const fn none() -> Self

Creates a new AtomicOptionBox with no value.

Equivalent to AtomicOptionBox::new(None), but can be used in const context.

§Examples
use mea::atomicbox::AtomicOptionBox;

static GLOBAL_BOX: AtomicOptionBox<u32> = AtomicOptionBox::none();
Source

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

Atomically set this AtomicOptionBox to other and return the previous value.

This does not allocate or free memory, and it neither clones nor drops any values. other is moved into self; the value previously in self is returned.

If other is Some, order must be either Ordering::AcqRel or Ordering::SeqCst, as other values would not be safe if T contains any data.

If other is None, order must be Ordering::Acquire, Ordering::AcqRel, or Ordering::SeqCst,

§Panics

Panics if order is not one of the allowed values.

§Examples
use std::sync::atomic::Ordering;

use mea::atomicbox::AtomicOptionBox;

let atom = AtomicOptionBox::new(None);
let prev_value = atom.swap(Some(Box::new("ok")), Ordering::AcqRel);
assert_eq!(prev_value, None);
Source

pub fn store(&self, other: Option<Box<T>>, order: Ordering)

Atomically set this AtomicOptionBox to other and drop the previous value.

The AtomicOptionBox takes ownership of other.

If other is Some, order must be either Ordering::AcqRel or Ordering::SeqCst, as other values would not be safe if T contains any data.

If other is None, order must be Ordering::Acquire, Ordering::AcqRel, or Ordering::SeqCst,

§Panics

Panics if order is not one of the allowed values.

§Examples
use std::sync::atomic::Ordering;

use mea::atomicbox::AtomicOptionBox;

let atom = AtomicOptionBox::new(None);
atom.store(Some(Box::new("ok")), Ordering::AcqRel);
assert_eq!(atom.into_inner(), Some(Box::new("ok")));
Source

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

Atomically set this AtomicOptionBox to None and return the previous value.

This does not allocate or free memory, and it neither clones nor drops any values. It is equivalent to calling self.swap(None, order)

order must be Ordering::Acquire, Ordering::AcqRel, or Ordering::SeqCst, as other values would not be safe if T contains any data.

§Panics

Panics if order is not one of the allowed values.

§Examples
use std::sync::atomic::Ordering;

use mea::atomicbox::AtomicOptionBox;

let atom = AtomicOptionBox::new(Some(Box::new("ok")));
let prev_value = atom.take(Ordering::AcqRel);
assert!(prev_value.is_some());
let prev_value = atom.take(Ordering::AcqRel);
assert!(prev_value.is_none());
Source

pub fn swap_mut(&self, other: &mut Option<Box<T>>, order: Ordering)

Atomically swaps the contents of this AtomicOptionBox with the contents of other.

This does not allocate or free memory, and it neither clones nor drops any values. The pointers in *other and self are simply exchanged.

If other is Some, order must be either Ordering::AcqRel or Ordering::SeqCst, as other values would not be safe if T contains any data.

If other is None, order must be Ordering::Acquire, Ordering::AcqRel, or Ordering::SeqCst,

§Panics

Panics if order is not one of the allowed values.

§Examples
use std::sync::atomic::Ordering;

use mea::atomicbox::AtomicOptionBox;

let atom = AtomicOptionBox::new(None);
let mut boxed = Some(Box::new("ok"));
let prev_value = atom.swap_mut(&mut boxed, Ordering::AcqRel);
assert_eq!(boxed, None);
Source

pub fn into_inner(self) -> Option<Box<T>>

Consume this AtomicOptionBox, returning the last option value it contained.

§Examples
use mea::atomicbox::AtomicOptionBox;

let atom = AtomicOptionBox::new(Some(Box::new("hello")));
assert_eq!(atom.into_inner(), Some(Box::new("hello")));
Source

pub fn get_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the contained value.

This is safe because it borrows the AtomicOptionBox mutably, which ensures that no other threads can concurrently access either the atomic pointer field or the boxed data it points to.

Trait Implementations§

Source§

impl<T> Debug for AtomicOptionBox<T>

Source§

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

The {:?} format of an AtomicOptionBox<T> looks like "AtomicOptionBox(0x12341234)" or "AtomicOptionBox(None)".

The address is the address of the Box allocation, if any, not the address of the AtomicOptionBox.

Source§

impl<T> Default for AtomicOptionBox<T>

Source§

fn default() -> AtomicOptionBox<T>

The default AtomicOptionBox<T> value is AtomicBox::new(None).

Source§

impl<T> Drop for AtomicOptionBox<T>

Source§

fn drop(&mut self)

Dropping an AtomicOptionBox<T> drops the final Box<T> value (if any) stored in it.

Source§

impl<T> Sync for AtomicOptionBox<T>
where T: Send,

Mark AtomicOptionBox<T> as safe to share across threads.

This is safe because shared access to an AtomicOptionBox<T> does not provide shared access to any T value. However, it does provide the ability to get a Box<T> from another thread, so T: Send is required.

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicOptionBox<T>

§

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

§

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

§

impl<T> Unpin for AtomicOptionBox<T>

§

impl<T> UnwindSafe for AtomicOptionBox<T>

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.