Skip to main content

VariableElements

Struct VariableElements 

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

Source

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.

Source

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.

Source

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.

Source

pub const fn size(&self) -> u32

Source

pub fn capacity(&self) -> u32

Capacity in bytes

Source

pub const fn count(&self) -> u32

Number of elements

Source

pub const fn get_initialized(&self) -> &[Element]

Try to get access to initialized elements

Source

pub fn get_initialized_mut(&mut self) -> &mut [Element]

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

Source

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.

Source

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.

Source

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

Source

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

Source

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

Source

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,

Source§

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

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

impl<Element, const RECOMMENDED_ALLOCATION: u32> IoType for VariableElements<Element, RECOMMENDED_ALLOCATION>
where Element: 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 = Element

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<Element, const RECOMMENDED_ALLOCATION: u32> IoTypeOptional for VariableElements<Element, RECOMMENDED_ALLOCATION>
where Element: TrivialType, [(); _]:,

Auto Trait Implementations§

§

impl<Element, const RECOMMENDED_ALLOCATION: u32 = 0> !Send for VariableElements<Element, RECOMMENDED_ALLOCATION>

§

impl<Element, const RECOMMENDED_ALLOCATION: u32 = 0> !Sync for VariableElements<Element, RECOMMENDED_ALLOCATION>

§

impl<Element, const RECOMMENDED_ALLOCATION: u32> Freeze for VariableElements<Element, RECOMMENDED_ALLOCATION>
where NonNull<Element>: Freeze, [(); _]: Freeze,

§

impl<Element, const RECOMMENDED_ALLOCATION: u32> RefUnwindSafe for VariableElements<Element, RECOMMENDED_ALLOCATION>
where NonNull<Element>: RefUnwindSafe, [(); _]: RefUnwindSafe,

§

impl<Element, const RECOMMENDED_ALLOCATION: u32> Unpin for VariableElements<Element, RECOMMENDED_ALLOCATION>
where NonNull<Element>: Unpin, [(); _]: Unpin,

§

impl<Element, const RECOMMENDED_ALLOCATION: u32> UnsafeUnpin for VariableElements<Element, RECOMMENDED_ALLOCATION>
where NonNull<Element>: UnsafeUnpin, [(); _]: UnsafeUnpin,

§

impl<Element, const RECOMMENDED_ALLOCATION: u32> UnwindSafe for VariableElements<Element, RECOMMENDED_ALLOCATION>
where NonNull<Element>: 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.