AtomicBox

Struct AtomicBox 

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

Source

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

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

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

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

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

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

Returns a mutable reference to the contained value.

This is safe because it borrows the AtomicBox 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 AtomicBox<T>

Source§

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

The {:?} format of an AtomicBox<T> looks like "AtomicBox(0x12341234)". The address is the address of the Box allocation, not the address of the AtomicBox.

Source§

impl<T> Default for AtomicBox<T>
where Box<T>: Default,

Source§

fn default() -> AtomicBox<T>

The default AtomicBox<T> value boxes the default T value.

Source§

impl<T> Drop for AtomicBox<T>

Source§

fn drop(&mut self)

Dropping an AtomicBox<T> drops the final Box<T> value stored in it.

Source§

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.

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicBox<T>

§

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

§

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

§

impl<T> Unpin for AtomicBox<T>

§

impl<T> UnwindSafe for AtomicBox<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.