MonoBox

Struct MonoBox 

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

A MonoBox<T> is an atomic, lock-free, write-once Option<Box<T>>. Write-once means that a MonoBox can only transition from None to Some<Box<T>> once, and is then frozen in that state until destruction.

As a special case, when one has exclusive ownership over the MonoBox (evidenced by a &mut reference), it is possible to MonoBox::swap its contents with an arbitrary Option<Box<T>>. This non-monotonic operation is safe because the mutable references guarantees no other thread can observe the transition.

Implementations§

Source§

impl<T> MonoBox<T>

Source

pub fn new(inner: Option<Box<T>>) -> Self

Returns a fresh MonoBox that holds inner.

Use Default::default() or MonoBox::empty() for a None initial value.

Source

pub fn empty() -> Self

Returns a fresh MonoBox that holds None.

Source

pub fn is_none(&self) -> bool

Returns whether the MonoBox’s value is None.

Source

pub fn is_some(&self) -> bool

Returns whether the MonoBox’s value is Some.

Source

pub fn swap(&mut self, value: Option<Box<T>>) -> Option<Box<T>>

Returns the value previously stored in this MonoBox and replaces it with value.

Source

pub fn store(&self, value: Box<T>) -> Result<(), Box<T>>

Attempts to store value in this MonoBox. The operation succeeds iff it upgrades the MonoBox from None to Some.

Returns Ok when the store succeeds, and passes back value as Err otherwise.

Source

pub fn store_value(&self, value: T) -> bool

Attempts to store value in this MonoBox.

Returns true on success and false if there was already some value in the MonoBox.

Source

pub fn as_ref(&self) -> Option<&T>

Gets the value stored in this MonoBox, if any.

Source

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

Gets the value stored in this MonoBox, if any.

Source

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

Takes the value out of this MonoBox, leaving a None in its place.

Source

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

Consumes this MonoBox, returning the wrapped value, if any.

Source§

impl<T: Deref> MonoBox<T>

Source

pub fn as_deref(&self) -> Option<&T::Target>

Source§

impl<T: DerefMut> MonoBox<T>

Source

pub fn as_deref_mut(&mut self) -> Option<&mut T::Target>

Trait Implementations§

Source§

impl<T: Debug> Debug for MonoBox<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for MonoBox<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Drop for MonoBox<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T> From<&'a MonoBox<T>> for Option<&'a T>

Source§

fn from(mono: &'a MonoBox<T>) -> Option<&T>

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a mut MonoBox<T>> for Option<&'a mut T>

Source§

fn from(mono: &'a mut MonoBox<T>) -> Option<&mut T>

Converts to this type from the input type.
Source§

impl<T> From<Box<T>> for MonoBox<T>

Source§

fn from(value: Box<T>) -> MonoBox<T>

Converts to this type from the input type.
Source§

impl<T> From<MonoBox<T>> for MonoArc<T>

Source§

fn from(mono: MonoBox<T>) -> MonoArc<T>

Converts to this type from the input type.
Source§

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

Source§

fn from(mono: MonoBox<T>) -> Option<Box<T>>

Converts to this type from the input type.
Source§

impl<T> From<MonoBox<T>> for Option<T>

Source§

fn from(mono: MonoBox<T>) -> Option<T>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: Option<Box<T>>) -> MonoBox<T>

Converts to this type from the input type.
Source§

impl<T> From<Option<T>> for MonoBox<T>

Source§

fn from(value: Option<T>) -> MonoBox<T>

Converts to this type from the input type.
Source§

impl<T> From<T> for MonoBox<T>

Source§

fn from(value: T) -> MonoBox<T>

Converts to this type from the input type.
Source§

impl<T> Pointer for MonoBox<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for MonoBox<T>

§

impl<T> RefUnwindSafe for MonoBox<T>

§

impl<T> Send for MonoBox<T>

§

impl<T> Sync for MonoBox<T>

§

impl<T> Unpin for MonoBox<T>

§

impl<T> UnwindSafe for MonoBox<T>
where T: RefUnwindSafe,

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.