MaybeEmpty

Struct MaybeEmpty 

Source
pub struct MaybeEmpty<T>(/* private fields */)
where
    T: IsEmpty;
Expand description

A wrapper type to represent an instance of a type T that may be in a well-known “empty” state where its usual guarantees aren’t upheld.

This is similar to MaybeUninit in that it ensures that the underlying structure isn’t accessed until we’re confident it’s valid, but it’s different in that the memory is actually initialized. It’s just initialized to a known pattern that indicates an empty or null value. The specific pattern differs from type to type; individual types implement the IsEmpty trait to determine when the structure is valid.

This is also similar to Option. The main difference is that this is usable for complex structs defined in C++, while Option is only usable for pointers and Rust structs.

Implementations§

Source§

impl<T> MaybeEmpty<T>
where T: IsEmpty,

Source

pub fn is_empty(&self) -> bool

Returns whether this struct is “empty” according to its own internal logic.

Source

pub fn as_non_null(&self) -> NonNull<T>

Gets a pointer to the contained value. Reading from this pointer is unsafe but well-defined, since the underlying memory will either be a valid T or match the well-known empty pattern.

Source

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

If this isn’t empty, returns it. Otherwise, returns None.

Source

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

If this isn’t empty, returns it. Otherwise, returns None.

Trait Implementations§

Source§

impl<T> Debug for MaybeEmpty<T>
where T: IsEmpty + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for MaybeEmpty<T>
where T: Freeze,

§

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

§

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

§

impl<T> Sync for MaybeEmpty<T>
where T: Sync,

§

impl<T> Unpin for MaybeEmpty<T>
where T: Unpin,

§

impl<T> UnwindSafe for MaybeEmpty<T>
where T: UnwindSafe,

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.