pub struct MaybeData<Data>where
Data: TrivialType,{ /* private fields */ }Expand description
Wrapper type for Data that may or may not be filled with contents.
This is somewhat similar to VariableElements,
but instead of a variable number of elements, data structure allows either having it or not
having the contents, which is a simpler and more appropriate API in many cases.
Implementations§
Source§impl<Data> MaybeData<Data>where
Data: TrivialType,
Self: IoType,
impl<Data> MaybeData<Data>where
Data: TrivialType,
Self: IoType,
Sourcepub const fn from_ref(data: Option<&Data>) -> impl Deref<Target = Self> + '_
pub const fn from_ref(data: Option<&Data>) -> impl Deref<Target = Self> + '_
Create a new shared instance from provided data reference.
Sourcepub fn from_mut<'a>(
buffer: &'a mut Data,
size: &'a mut u32,
) -> Option<impl DerefMut<Target = Self> + IoTypeMut<Target = Self> + 'a>
pub fn from_mut<'a>( buffer: &'a mut Data, size: &'a mut u32, ) -> Option<impl DerefMut<Target = Self> + IoTypeMut<Target = Self> + 'a>
Create a new exclusive instance from provided data reference.
size can be either 0 or Data::SIZE, indicating that value is missing or present
accordingly.
Returns None if size is neither 0 nor Data::SIZE.
Sourcepub fn from_uninit<'a>(
uninit: &'a mut MaybeUninit<Data>,
size: &'a mut u32,
) -> Option<impl DerefMut<Target = Self> + IoTypeMut<Target = Self> + 'a>
pub fn from_uninit<'a>( uninit: &'a mut MaybeUninit<Data>, size: &'a mut u32, ) -> Option<impl DerefMut<Target = Self> + IoTypeMut<Target = Self> + 'a>
Create a new exclusive instance from provided uninitialized memory.
size must be 0 since data is not initialized yet.
Returns None if size != 0.
Sourcepub const fn get(&self) -> Option<&Data>
pub const fn get(&self) -> Option<&Data>
Try to get access to initialized Data, returns None if not initialized
Sourcepub fn get_mut(&mut self) -> Option<&mut Data>
pub fn get_mut(&mut self) -> Option<&mut Data>
Try to get exclusive access to initialized Data, returns None if not initialized
Sourcepub fn replace(&mut self, data: Data) -> &mut Data
pub fn replace(&mut self, data: Data) -> &mut Data
Initialize by inserting Data by value or replace existing value and return reference to it
Sourcepub fn get_mut_or_init_with<Init>(&mut self, init: Init) -> &mut Data
pub fn get_mut_or_init_with<Init>(&mut self, init: Init) -> &mut Data
Get exclusive access to initialized Data, running provided initialization function if
necessary.
init is expected to initialize provided memory and return a reference to it, like
MaybeUninit::write() does. If it returns a reference to some other Data instead, that
value is copied into this instance.
Sourcepub unsafe fn assume_init(&mut self) -> &mut Data
pub unsafe fn assume_init(&mut self) -> &mut Data
Trait Implementations§
Source§impl<Data> IoType for MaybeData<Data>
impl<Data> IoType for MaybeData<Data>
Source§const METADATA: &[u8]
const METADATA: &[u8]
IoTypeMetadataKind for encoding detailsSource§type PointerType = Data
type PointerType = Data
IoType representsSource§unsafe fn from_ptr<'a>(
ptr: &'a NonNull<Self::PointerType>,
size: &'a u32,
capacity: u32,
) -> impl Deref<Target = Self> + 'a
unsafe fn from_ptr<'a>( ptr: &'a NonNull<Self::PointerType>, size: &'a u32, capacity: u32, ) -> impl Deref<Target = Self> + 'a
Source§unsafe fn from_mut_ptr<'a>(
ptr: &'a mut NonNull<Self::PointerType>,
size: &'a mut u32,
capacity: u32,
) -> impl DerefMut<Target = Self> + 'a
unsafe fn from_mut_ptr<'a>( ptr: &'a mut NonNull<Self::PointerType>, size: &'a mut u32, capacity: u32, ) -> impl DerefMut<Target = Self> + 'a
Source§unsafe fn try_from_ptr<'a>(
ptr: &'a NonNull<Self::PointerType>,
size: &'a u32,
capacity: u32,
) -> Option<impl Deref<Target = Self> + 'a>
unsafe fn try_from_ptr<'a>( ptr: &'a NonNull<Self::PointerType>, size: &'a u32, capacity: u32, ) -> Option<impl Deref<Target = Self> + 'a>
Source§unsafe fn try_from_mut_ptr<'a>(
ptr: &'a mut NonNull<Self::PointerType>,
size: &'a mut u32,
capacity: u32,
) -> Option<impl DerefMut<Target = Self> + 'a>
unsafe fn try_from_mut_ptr<'a>( ptr: &'a mut NonNull<Self::PointerType>, size: &'a mut u32, capacity: u32, ) -> Option<impl DerefMut<Target = Self> + 'a>
Self::try_from_ptr(), but returns an exclusive reference. Read more