Skip to main content

spl_list_view/
list_trait.rs

1use {
2    crate::ListView, bytemuck::Pod, core::ops::Deref, solana_program_error::ProgramError,
3    spl_pod::pod_length::PodLength,
4};
5
6/// A trait to abstract the shared, read-only behavior
7/// between `ListViewReadOnly` and `ListViewMut`.
8pub trait List: Deref<Target = [Self::Item]> {
9    /// The type of the items stored in the list.
10    type Item: Pod;
11    /// Length prefix type used (`PodU16`, `PodU32`, …).
12    type Length: PodLength;
13
14    /// Returns the total number of items that can be stored in the list.
15    fn capacity(&self) -> usize;
16
17    /// Returns the number of **bytes currently occupied** by the live elements
18    fn bytes_used(&self) -> Result<usize, ProgramError> {
19        ListView::<Self::Item, Self::Length>::size_of(self.len())
20    }
21
22    /// Returns the number of **bytes reserved** by the entire backing buffer.
23    fn bytes_allocated(&self) -> Result<usize, ProgramError> {
24        ListView::<Self::Item, Self::Length>::size_of(self.capacity())
25    }
26}