Skip to main content

MaybeData

Struct MaybeData 

Source
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,

Source

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

Create a new shared instance from provided data reference.

Source

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.

Source

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.

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<Data> Debug for MaybeData<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§

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> + '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> + '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> + '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> + '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§

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

Auto Trait Implementations§

§

impl<Data> !Send for MaybeData<Data>

§

impl<Data> !Sync for MaybeData<Data>

§

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

§

impl<Data> RefUnwindSafe for MaybeData<Data>

§

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

§

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

§

impl<Data> UnwindSafe for MaybeData<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> IoTypeMut for T
where T: IoType,

Source§

type Target = T

The type of the value
Source§

fn io_type_mut(&mut self) -> &mut <T as IoTypeMut>::Target

Exclusive access to the value
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, <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.