#[repr(C)]pub struct VariableElements<Element, const RECOMMENDED_ALLOCATION: u32 = 0>where
Element: TrivialType,{ /* private fields */ }
Expand description
Container for storing variable number of elements.
RECOMMENDED_ALLOCATION
is what is being used when a host needs to allocate memory for call
into guest, but guest may receive an allocation with more or less memory in practice depending
on other circumstances, like when called from another contract with specific allocation
specified.
Implementations§
Source§impl<Element, const RECOMMENDED_ALLOCATION: u32> VariableElements<Element, RECOMMENDED_ALLOCATION>where
Element: TrivialType,
impl<Element, const RECOMMENDED_ALLOCATION: u32> VariableElements<Element, RECOMMENDED_ALLOCATION>where
Element: TrivialType,
Sourcepub const fn from_buffer<'a>(
buffer: &'a [<Self as IoType>::PointerType],
size: &'a u32,
) -> impl Deref<Target = Self> + 'a
pub const fn from_buffer<'a>( buffer: &'a [<Self as IoType>::PointerType], size: &'a u32, ) -> impl Deref<Target = Self> + 'a
Create a new shared instance from provided memory buffer.
NOTE: size is specified in bytes, not elements.
§Panics
Panics if buffer.len * Element::SIZE() != size
Sourcepub fn from_buffer_mut<'a>(
buffer: &'a mut [<Self as IoType>::PointerType],
size: &'a mut u32,
) -> impl DerefMut<Target = Self> + 'a
pub fn from_buffer_mut<'a>( buffer: &'a mut [<Self as IoType>::PointerType], size: &'a mut u32, ) -> impl DerefMut<Target = Self> + 'a
Create a new exclusive instance from provided memory buffer.
§Panics
Panics if buffer.len() * Element::SIZE != size
Sourcepub fn from_uninit<'a, const CAPACITY: usize>(
uninit: &'a mut [MaybeUninit<<Self as IoType>::PointerType>; CAPACITY],
size: &'a mut u32,
) -> impl DerefMut<Target = Self> + 'a
pub fn from_uninit<'a, const CAPACITY: usize>( uninit: &'a mut [MaybeUninit<<Self as IoType>::PointerType>; CAPACITY], size: &'a mut u32, ) -> impl DerefMut<Target = Self> + 'a
Create a new shared instance from provided memory buffer.
NOTE: size is specified in bytes, not elements.
§Panics
Panics if size > CAPACITY
or size % Element::SIZE != 0
pub const fn size(&self) -> u32
Sourcepub const fn get_initialized(&self) -> &[Element]
pub const fn get_initialized(&self) -> &[Element]
Try to get access to initialized elements
Sourcepub fn get_initialized_mut(&mut self) -> &mut [Element]
pub fn get_initialized_mut(&mut self) -> &mut [Element]
Try to get exclusive access to initialized Data
, returns None
if not initialized
Sourcepub fn append(&mut self, elements: &[Element]) -> bool
pub fn append(&mut self, elements: &[Element]) -> bool
Append some elements by using more of allocated, but currently unused elements.
true
is returned on success, but if there isn’t enough unused elements left, false
is.
Sourcepub fn truncate(&mut self, new_size: u32) -> bool
pub fn truncate(&mut self, new_size: u32) -> bool
Truncate internal initialized bytes to this size.
Returns true
on success or false
if new_size
is larger than Self::size()
or not a
multiple of Element::SIZE
.
Sourcepub fn copy_from(&mut self, src: &Self) -> bool
pub fn copy_from(&mut self, src: &Self) -> bool
Copy contents from another instance.
Returns false
if actual capacity of the instance is not enough to copy contents of src
Sourcepub fn as_mut_ptr(&mut self) -> &mut NonNull<Element>
pub fn as_mut_ptr(&mut self) -> &mut NonNull<Element>
Get exclusive access to the underlying pointer with no checks.
Can be used for initialization with Self::assume_init()
called afterward to confirm how
many bytes are in use right now.
Sourcepub fn cast_ref<const DIFFERENT_RECOMMENDED_ALLOCATION: u32>(
&self,
) -> &VariableElements<Element, DIFFERENT_RECOMMENDED_ALLOCATION>
pub fn cast_ref<const DIFFERENT_RECOMMENDED_ALLOCATION: u32>( &self, ) -> &VariableElements<Element, DIFFERENT_RECOMMENDED_ALLOCATION>
Cast a shared reference to this instance into a reference to an instance of a different recommended allocation
Sourcepub fn cast_mut<const DIFFERENT_RECOMMENDED_ALLOCATION: u32>(
&mut self,
) -> &mut VariableElements<Element, DIFFERENT_RECOMMENDED_ALLOCATION>
pub fn cast_mut<const DIFFERENT_RECOMMENDED_ALLOCATION: u32>( &mut self, ) -> &mut VariableElements<Element, DIFFERENT_RECOMMENDED_ALLOCATION>
Cast an exclusive reference to this instance into a reference to an instance of a different recommended allocation
Sourcepub unsafe fn assume_init(&mut self, size: u32) -> Option<&mut [Element]>
pub unsafe fn assume_init(&mut self, size: u32) -> Option<&mut [Element]>
Assume that the first size
are initialized and can be read.
Returns Some(initialized_elements)
on success or None
if size
is larger than its
capacity or not a multiple of Element::SIZE
.
§Safety
Caller must ensure size
is actually initialized
Trait Implementations§
Source§impl<Element, const RECOMMENDED_ALLOCATION: u32> Debug for VariableElements<Element, RECOMMENDED_ALLOCATION>where
Element: TrivialType + Debug,
impl<Element, const RECOMMENDED_ALLOCATION: u32> Debug for VariableElements<Element, RECOMMENDED_ALLOCATION>where
Element: TrivialType + Debug,
Source§impl<Element, const RECOMMENDED_ALLOCATION: u32> IoType for VariableElements<Element, RECOMMENDED_ALLOCATION>where
Element: TrivialType,
impl<Element, const RECOMMENDED_ALLOCATION: u32> IoType for VariableElements<Element, RECOMMENDED_ALLOCATION>where
Element: TrivialType,
Source§const METADATA: &[u8]
const METADATA: &[u8]
IoTypeMetadataKind
for encoding detailsSource§type PointerType = Element
type PointerType = Element
IoType
represents