pub struct AtomicBox<T> { /* private fields */ }Expand description
A type that holds a single Box<T> value and can be safely shared between threads.
Implementations§
Source§impl<T> AtomicBox<T>
impl<T> AtomicBox<T>
Sourcepub fn new(value: Box<T>) -> AtomicBox<T>
pub fn new(value: Box<T>) -> AtomicBox<T>
Creates a new AtomicBox with the given value.
§Examples
use mea::atomicbox::AtomicBox;
let atomic_box = AtomicBox::new(Box::new(0));Sourcepub fn swap(&self, other: Box<T>) -> Box<T>
pub fn swap(&self, other: Box<T>) -> Box<T>
Atomically set this AtomicBox 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.
§Examples
use mea::atomicbox::AtomicBox;
let atom = AtomicBox::new(Box::new("one"));
let prev_value = atom.swap(Box::new("two"));
assert_eq!(*prev_value, "one");Sourcepub fn store(&self, other: Box<T>)
pub fn store(&self, other: Box<T>)
Atomically set this AtomicBox to other and drop its previous value.
The AtomicBox takes ownership of other.
§Examples
use mea::atomicbox::AtomicBox;
let atom = AtomicBox::new(Box::new("one"));
atom.store(Box::new("two"));
assert_eq!(atom.into_inner(), Box::new("two"));Sourcepub fn swap_mut(&self, other: &mut Box<T>)
pub fn swap_mut(&self, other: &mut Box<T>)
Atomically swaps the contents of this AtomicBox 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.
§Examples
use mea::atomicbox::AtomicBox;
let atom = AtomicBox::new(Box::new("one"));
let mut boxed = Box::new("two");
atom.swap_mut(&mut boxed);
assert_eq!(*boxed, "one");Sourcepub fn into_inner(self) -> Box<T>
pub fn into_inner(self) -> Box<T>
Consume this AtomicBox, returning the last box value it contained.
§Examples
use mea::atomicbox::AtomicBox;
let atom = AtomicBox::new(Box::new("hello"));
assert_eq!(atom.into_inner(), Box::new("hello"));Trait Implementations§
impl<T> Sync for AtomicBox<T>where
T: Send,
Mark AtomicBox<T> as safe to share across threads.
This is safe because shared access to an AtomicBox<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.