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,
impl<T> MaybeEmpty<T>where
T: IsEmpty,
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether this struct is “empty” according to its own internal logic.
Sourcepub fn as_non_null(&self) -> NonNull<T>
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.
Sourcepub fn as_option(&self) -> Option<&T>
pub fn as_option(&self) -> Option<&T>
If this isn’t empty, returns it. Otherwise, returns None.
Sourcepub fn as_option_mut(&mut self) -> Option<&mut T>
pub fn as_option_mut(&mut self) -> Option<&mut T>
If this isn’t empty, returns it. Otherwise, returns None.