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,
Self: IoType,
impl<Element, const RECOMMENDED_ALLOCATION: u32> VariableElements<Element, RECOMMENDED_ALLOCATION>where
Element: TrivialType,
Self: IoType,
Sourcepub const fn from_buffer<'a>(
buffer: &'a [Element],
size: &'a u32,
) -> Option<impl Deref<Target = Self> + 'a>
pub const fn from_buffer<'a>( buffer: &'a [Element], size: &'a u32, ) -> Option<impl Deref<Target = Self> + 'a>
Create a new shared instance from provided memory buffer.
NOTE: size is specified in bytes, not elements.
Returns None if size_of_val(buffer) != size.
Sourcepub fn from_buffer_mut<'a>(
buffer: &'a mut [Element],
size: &'a mut u32,
) -> Option<impl DerefMut<Target = Self> + IoTypeMut<Target = Self> + 'a>
pub fn from_buffer_mut<'a>( buffer: &'a mut [Element], size: &'a mut u32, ) -> Option<impl DerefMut<Target = Self> + IoTypeMut<Target = Self> + 'a>
Create a new exclusive instance from provided memory buffer.
NOTE: size is specified in bytes, not elements.
Returns None if size_of_val(buffer) != size.
Sourcepub fn from_uninit<'a>(
uninit: &'a mut [MaybeUninit<Element>],
size: &'a mut u32,
) -> Option<impl DerefMut<Target = Self> + IoTypeMut<Target = Self> + 'a>
pub fn from_uninit<'a>( uninit: &'a mut [MaybeUninit<Element>], size: &'a mut u32, ) -> Option<impl DerefMut<Target = Self> + IoTypeMut<Target = Self> + 'a>
Create a new exclusive instance from provided uninitialized memory buffer.
size must be 0 since none of the elements are initialized yet, the whole buffer is used
as capacity.
Returns None if size != 0 or if the buffer is larger than u32::MAX bytes.
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 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>
impl<Element, const RECOMMENDED_ALLOCATION: u32> IoType for VariableElements<Element, RECOMMENDED_ALLOCATION>
Source§const METADATA: &[u8]
const METADATA: &[u8]
IoTypeMetadataKind for encoding detailsSource§type PointerType = Element
type PointerType = Element
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