Struct ListView

Source
pub struct ListView<T: Pod, L: PodLength = PodU32>(/* private fields */);
Expand description

An API for interpreting a raw buffer (&[u8]) as a variable-length collection of Pod elements.

ListView provides a safe, zero-copy, Vec-like interface for a slice of Pod data that resides in an external, pre-allocated &[u8] buffer. It does not own the buffer itself, but acts as a view over it, which can be read-only (ListViewReadOnly) or mutable (ListViewMut).

This is useful in environments where allocations are restricted or expensive, such as Solana programs, allowing for efficient reads and manipulation of dynamic-length data structures.

§Memory Layout

The structure assumes the underlying byte buffer is formatted as follows:

  1. Length: A length field of type L at the beginning of the buffer, indicating the number of currently active elements in the collection. Defaults to PodU32. The implementation uses padding to ensure that the data is correctly aligned for any Pod type.
  2. Padding: Optional padding bytes to ensure proper alignment of the data.
  3. Data: The remaining part of the buffer, which is treated as a slice of T elements. The capacity of the collection is the number of T elements that can fit into this data portion.

Implementations§

Source§

impl<T: Pod, L: PodLength> ListView<T, L>

Source

pub fn size_of(num_items: usize) -> Result<usize, ProgramError>

Calculate the total byte size for a ListView holding num_items. This includes the length prefix, padding, and data.

Source

pub fn unpack(buf: &[u8]) -> Result<ListViewReadOnly<'_, T, L>, ProgramError>

Unpack a read-only buffer into a ListViewReadOnly

Source

pub fn unpack_mut(buf: &mut [u8]) -> Result<ListViewMut<'_, T, L>, ProgramError>

Unpack the mutable buffer into a mutable ListViewMut

Source

pub fn init(buf: &mut [u8]) -> Result<ListViewMut<'_, T, L>, ProgramError>

Initialize a buffer: sets length = 0 and returns a mutable ListViewMut.

Auto Trait Implementations§

§

impl<T, L> Freeze for ListView<T, L>

§

impl<T, L> RefUnwindSafe for ListView<T, L>

§

impl<T, L> Send for ListView<T, L>
where T: Send, L: Send,

§

impl<T, L> Sync for ListView<T, L>
where T: Sync, L: Sync,

§

impl<T, L> Unpin for ListView<T, L>
where T: Unpin, L: Unpin,

§

impl<T, L> UnwindSafe for ListView<T, L>
where T: UnwindSafe, L: 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V