ab_contracts_io_type::maybe_data

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 VariableBytes, but instead of variable size data structure allows to either have it or not have the contents or not have it, which is a simpler and more convenient API that is also sufficient in many cases.

Implementations§

Source§

impl<Data> MaybeData<Data>
where Data: TrivialType,

Source

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

Create a new shared instance from provided data reference.

Source

pub fn from_buffer_mut<'a>( buffer: &'a mut Data, size: &'a mut u32, ) -> impl DerefMut<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.

§Panics

Panics if size != 0 && size != Data::SIZE

Source

pub fn from_uninit<'a>( uninit: &'a mut MaybeUninit<Data>, size: &'a mut u32, ) -> impl Deref<Target = Self> + 'a

Create a new shared instance from provided memory buffer.

size must be 0.

§Panics

Panics if size != 0

Source

pub 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<'a, Init>(&'a mut self, init: Init) -> &'a mut Data
where Init: FnOnce(NonNull<Data>) -> &'a mut Data,

Get exclusive access to initialized Data, running provided initialization function if necessary

Source

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

Assume value is initialized

§Safety

Caller must ensure Data is actually properly initialized

Trait Implementations§

Source§

impl<Data> IoType for MaybeData<Data>
where Data: TrivialType,

Source§

const METADATA: &[u8] = Data::METADATA

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

type PointerType = Data

Pointer with trivial type that this IoType represents
Source§

fn size(&self) -> u32

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

unsafe fn size_ptr(&self) -> impl Deref<Target = NonNull<u32>>

Pointer to the number of bytes that are currently used to store data. Read more
Source§

unsafe fn size_mut_ptr(&mut self) -> impl DerefMut<Target = *mut u32>

An exclusive pointer to the number of bytes that are currently used to store data. Read more
Source§

fn capacity(&self) -> u32

Number of bytes are allocated right now
Source§

unsafe fn capacity_ptr(&self) -> impl Deref<Target = NonNull<u32>>

Number of bytes are allocated right now Read more
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 *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 as_ptr(&self) -> impl Deref<Target = NonNull<Self::PointerType>>

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

unsafe fn as_mut_ptr( &mut self, ) -> impl DerefMut<Target = NonNull<Self::PointerType>>

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

impl<Data> IoTypeOptional for MaybeData<Data>
where Data: TrivialType,

Auto Trait Implementations§

§

impl<Data> Freeze for MaybeData<Data>

§

impl<Data> RefUnwindSafe for MaybeData<Data>
where Data: RefUnwindSafe,

§

impl<Data> !Send for MaybeData<Data>

§

impl<Data> !Sync for MaybeData<Data>

§

impl<Data> Unpin for MaybeData<Data>

§

impl<Data> UnwindSafe for MaybeData<Data>
where Data: RefUnwindSafe,

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.