Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
node_modules
test-ledger
dist
.idea
15 changes: 14 additions & 1 deletion pod/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Error types
use solana_program_error::{ProgramError, ToStr};
use {
solana_program_error::{ProgramError, ToStr},
std::num::TryFromIntError,
};

/// Errors that may be returned by the spl-pod library.
#[repr(u32)]
Expand All @@ -22,6 +25,9 @@ pub enum PodSliceError {
/// Provided byte buffer too large for expected type
#[error("Provided byte buffer too large for expected type")]
BufferTooLarge,
/// An integer conversion failed because the value was out of range for the target type
#[error("An integer conversion failed because the value was out of range for the target type")]
ValueOutOfRange,
}

impl From<PodSliceError> for ProgramError {
Expand All @@ -36,6 +42,13 @@ impl ToStr for PodSliceError {
PodSliceError::CalculationFailure => "Error in checked math operation",
PodSliceError::BufferTooSmall => "Provided byte buffer too small for expected type",
PodSliceError::BufferTooLarge => "Provided byte buffer too large for expected type",
PodSliceError::ValueOutOfRange => "An integer conversion failed because the value was out of range for the target type"
}
}
}

impl From<TryFromIntError> for PodSliceError {
fn from(_: TryFromIntError) -> Self {
PodSliceError::ValueOutOfRange
}
}
2 changes: 2 additions & 0 deletions pod/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

pub mod bytemuck;
pub mod error;
pub mod list;
pub mod option;
pub mod optional_keys;
pub mod pod_length;
pub mod primitives;
pub mod slice;

Expand Down
44 changes: 44 additions & 0 deletions pod/src/list/list_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use {
crate::{list::ListView, pod_length::PodLength},
bytemuck::Pod,
solana_program_error::ProgramError,
std::slice::Iter,
};

/// A trait to abstract the shared, read-only behavior
/// between `ListViewReadOnly` and `ListViewMut`.
pub trait List {
/// The type of the items stored in the list.
type Item: Pod;
/// Length prefix type used (`PodU16`, `PodU32`, …).
type Length: PodLength;

/// Returns the number of items in the list.
fn len(&self) -> usize;

/// Returns `true` if the list contains no items.
fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns the total number of items that can be stored in the list.
fn capacity(&self) -> usize;

/// Returns a read-only slice of the items currently in the list.
fn as_slice(&self) -> &[Self::Item];

/// Returns a read-only iterator over the list.
fn iter(&self) -> Iter<'_, Self::Item> {
self.as_slice().iter()
}

/// Returns the number of **bytes currently occupied** by the live elements
fn bytes_used(&self) -> Result<usize, ProgramError> {
ListView::<Self::Item, Self::Length>::size_of(self.len())
}

/// Returns the number of **bytes reserved** by the entire backing buffer.
fn bytes_allocated(&self) -> Result<usize, ProgramError> {
ListView::<Self::Item, Self::Length>::size_of(self.capacity())
}
}
Loading