spl_pod/list/
list_trait.rs

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