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:
- Length: A length field of type
Lat the beginning of the buffer, indicating the number of currently active elements in the collection. Defaults toPodU32. The implementation uses padding to ensure that the data is correctly aligned for anyPodtype. - Padding: Optional padding bytes to ensure proper alignment of the data.
- Data: The remaining part of the buffer, which is treated as a slice
of
Telements. The capacity of the collection is the number ofTelements that can fit into this data portion.
Implementations§
Source§impl<T: Pod, L: PodLength> ListView<T, L>
impl<T: Pod, L: PodLength> ListView<T, L>
Sourcepub fn size_of(num_items: usize) -> Result<usize, ProgramError>
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.
Sourcepub fn unpack(buf: &[u8]) -> Result<ListViewReadOnly<'_, T, L>, ProgramError>
pub fn unpack(buf: &[u8]) -> Result<ListViewReadOnly<'_, T, L>, ProgramError>
Unpack a read-only buffer into a ListViewReadOnly
Sourcepub fn unpack_mut(buf: &mut [u8]) -> Result<ListViewMut<'_, T, L>, ProgramError>
pub fn unpack_mut(buf: &mut [u8]) -> Result<ListViewMut<'_, T, L>, ProgramError>
Unpack the mutable buffer into a mutable ListViewMut
Sourcepub fn init(buf: &mut [u8]) -> Result<ListViewMut<'_, T, L>, ProgramError>
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>where
T: RefUnwindSafe,
L: RefUnwindSafe,
impl<T, L> Send for ListView<T, L>
impl<T, L> Sync for ListView<T, L>
impl<T, L> Unpin for ListView<T, L>
impl<T, L> UnwindSafe for ListView<T, L>where
T: UnwindSafe,
L: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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