Skip to main content

MaybeData

Struct MaybeData 

Source
pub struct MaybeData<'a, 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.

It is a view of memory that is borrowed for 'a.

Implementations§

Source§

impl<'a, Data> MaybeData<'a, Data>
where Data: TrivialType, Self: IoType,

Source

pub const fn from_ref(data: Option<&'a Data>) -> impl Deref<Target = Self> + 'a

Create a new shared instance from provided data reference.

Source

pub fn from_mut(buffer: &'a mut Data, size: &'a mut u32) -> Option<Self>

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.

Source

pub fn from_uninit( uninit: &'a mut MaybeUninit<Data>, size: &'a mut u32, ) -> Option<Self>

Create a new exclusive instance from provided uninitialized memory.

size must be 0 since data is not initialized yet.

Returns None if size != 0.

Source

pub const fn get(&self) -> Option<&Data>

Try to get access to initialized Data, returns None if not initialized

Source

pub fn get_mut(&mut self) -> Option<&mut Data>

Try to get exclusive access to initialized Data, returns None if not initialized

Source

pub fn replace(&mut self, data: Data) -> &mut Data

Initialize by inserting Data by value or replace existing value and return reference to it

Source

pub fn remove(&mut self)

Remove Data inside and turn instance back into uninitialized

Source

pub fn get_mut_or_init_with<Init>(&mut self, init: Init) -> &mut Data
where Init: FnOnce(&mut MaybeUninit<Data>) -> &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.

Source

pub unsafe fn assume_init(&mut self) -> &mut Data

Assume value is initialized

§Safety

Caller must ensure Data is actually properly initialized

Source§

impl<Data> MaybeData<'_, Data>
where Data: TrivialType + Default, Self: IoType,

Source

pub fn get_mut_or_default(&mut self) -> &mut Data

Get exclusive access to initialized Data, initializing with default value if necessary

Trait Implementations§

Source§

impl<'a, Data> Debug for MaybeData<'a, Data>
where Data: TrivialType + Debug,

Source§

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

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

impl<Data> IoType for MaybeData<'_, Data>
where Data: TrivialType, [(); _]:,

Source§

const METADATA: &[u8]

Data structure metadata in binary form, describing shape and types of the contents, see IoTypeMetadataKind for encoding details
Source§

type PointerType = Data

Pointer with a trivial type that this IoType represents
Source§

type WithLifetime<'b> = MaybeData<'b, Data>

The same type, viewing memory borrowed for 'b. Read more
Source§

type Reborrowed<'b> = MaybeData<'b, Data>
where Self: 'b,

Exclusive access to a value for 'b, which Self::reborrow_mut() returns. Read more
Source§

fn size(&self) -> u32

Number of bytes that are currently used to store data
Source§

fn capacity(&self) -> u32

Number of bytes are allocated right now
Source§

unsafe fn set_size(&mut self, size: u32)

Set the number of used bytes. Read more
Source§

unsafe fn from_ptr<'a>( ptr: &'a NonNull<Self::PointerType>, size: &'a u32, capacity: u32, ) -> impl Deref<Target = Self::WithLifetime<'a>> + 'a

Create a reference to a type, which is represented by provided memory. Read more
Source§

unsafe fn from_mut_ptr<'a>( ptr: &'a mut NonNull<Self::PointerType>, size: &'a mut u32, capacity: u32, ) -> impl DerefMut<Target = Self::WithLifetime<'a>> + 'a

Create a mutable reference to a type, which is represented by provided memory. Read more
Source§

unsafe fn try_from_ptr<'a>( ptr: &'a NonNull<Self::PointerType>, size: &'a u32, capacity: u32, ) -> Option<impl Deref<Target = Self::WithLifetime<'a>> + 'a>

Create a reference to a type, which is represented by provided memory, if the memory holds a valid value. Read more
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::WithLifetime<'a>> + 'a>

Like Self::try_from_ptr(), but returns an exclusive reference. Read more
Source§

fn as_ptr(&self) -> NonNull<Self::PointerType>

Get a raw pointer to the underlying data. Read more
Source§

fn as_mut_ptr(&mut self) -> NonNull<Self::PointerType>

Get an exclusive raw pointer to the underlying data. Read more
Source§

fn reborrow_mut(&mut self) -> Self::Reborrowed<'_>

Exclusive access to this value for the duration of the borrow. Read more
Source§

impl<Data> IoTypeOptional for MaybeData<'_, Data>
where Data: TrivialType, [(); _]:,

Auto Trait Implementations§

§

impl<'a, Data> !Send for MaybeData<'a, Data>

§

impl<'a, Data> !Sync for MaybeData<'a, Data>

§

impl<'a, Data> Forget for MaybeData<'a, Data>

§

impl<'a, Data> Freeze for MaybeData<'a, Data>
where NonNull<Data>: Freeze, [(); _]: Freeze,

§

impl<'a, Data> RefUnwindSafe for MaybeData<'a, Data>

§

impl<'a, Data> Unpin for MaybeData<'a, Data>
where NonNull<Data>: Unpin, [(); _]: Unpin,

§

impl<'a, Data> UnsafeUnpin for MaybeData<'a, Data>
where NonNull<Data>: UnsafeUnpin, [(); _]: UnsafeUnpin,

§

impl<'a, Data> UnwindSafe for MaybeData<'a, Data>
where NonNull<Data>: UnwindSafe, [(); _]: 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.