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>
impl<T> AtomicOptionBox<T>
Sourcepub fn new(value: Option<Box<T>>) -> AtomicOptionBox<T>
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)));Sourcepub const fn none() -> Self
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();Sourcepub fn swap(&self, other: Option<Box<T>>, order: Ordering) -> Option<Box<T>>
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);Sourcepub fn store(&self, other: Option<Box<T>>, order: Ordering)
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")));Sourcepub fn take(&self, order: Ordering) -> Option<Box<T>>
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());Sourcepub fn swap_mut(&self, other: &mut Option<Box<T>>, order: Ordering)
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);Sourcepub fn into_inner(self) -> Option<Box<T>>
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")));Trait Implementations§
Source§impl<T> Debug for AtomicOptionBox<T>
impl<T> Debug for AtomicOptionBox<T>
Source§impl<T> Default for AtomicOptionBox<T>
impl<T> Default for AtomicOptionBox<T>
Source§fn default() -> AtomicOptionBox<T>
fn default() -> AtomicOptionBox<T>
The default AtomicOptionBox<T> value is AtomicBox::new(None).
Source§impl<T> Drop for AtomicOptionBox<T>
impl<T> Drop for AtomicOptionBox<T>
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.