Skip to content
Open
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
61 changes: 51 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions pod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ license = "Apache-2.0"
edition = "2021"

[features]
bytemuck = ["dep:bytemuck", "dep:bytemuck_derive", "solana-address/bytemuck"]
serde = ["dep:serde", "dep:serde_derive", "solana-address/serde"]
borsh = ["dep:borsh", "solana-address/borsh"]
wincode = ["dep:wincode", "dep:wincode-derive"]
serde-traits = ["dep:serde"]
borsh = ["dep:borsh", "solana-pubkey/borsh"]
wincode = ["dep:wincode"]

[dependencies]
borsh = { version = "1.5.7", default-features = false, features = ["derive", "unstable__schema"], optional = true }
bytemuck = { version = "1.23.2", optional = true }
bytemuck_derive = { version = "1.10.1", optional = true }
serde = { version = "1.0.228", default-features = false, optional = true, features = ["alloc"] }
serde_derive = { version = "1.0.228", optional = true }
solana-address = "2.2.0"
borsh = { version = "1.5.7", features = ["derive", "unstable__schema"], optional = true }
bytemuck = { version = "1.23.2" }
bytemuck_derive = { version = "1.10.1" }
num-derive = "0.4"
num_enum = "0.7"
num-traits = "0.2"
serde = { version = "1.0.228", optional = true }
wincode = { version = "0.4.4", features = ["derive"], optional = true }
solana-program-error = "3.0.0"
solana-program-option = "3.0.0"
wincode = { version = "0.4.4", default-features = false, optional = true }
wincode-derive = { version = "0.4.2", optional = true }
solana-pubkey = "3.0.0"
solana-zk-sdk = "4.0.0"
thiserror = "2.0"

[dev-dependencies]
base64 = { version = "0.22.1" }
serde_json = "1.0.145"
spl-pod = { path = ".", features = ["bytemuck", "wincode", "borsh"] }
spl-pod = { path = ".", features = ["wincode"] }
test-case = "3.3.1"
wincode = { version = "0.4.4", default-features = false, features = ["alloc"] }

[lib]
crate-type = ["lib"]
Expand Down
2 changes: 1 addition & 1 deletion pod/src/bytemuck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {bytemuck::Pod, solana_program_error::ProgramError};

/// On-chain size of a `Pod` type
pub const fn pod_get_packed_len<T: Pod>() -> usize {
core::mem::size_of::<T>()
std::mem::size_of::<T>()
}

/// Convert a `Pod` into a slice of bytes (zero copy)
Expand Down
54 changes: 54 additions & 0 deletions pod/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Error types
use {
solana_program_error::{ProgramError, ToStr},
std::num::TryFromIntError,
};

/// Errors that may be returned by the spl-pod library.
#[repr(u32)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
thiserror::Error,
num_enum::TryFromPrimitive,
num_derive::FromPrimitive,
)]
pub enum PodSliceError {
/// Error in checked math operation
#[error("Error in checked math operation")]
CalculationFailure,
/// Provided byte buffer too small for expected type
#[error("Provided byte buffer too small for expected type")]
BufferTooSmall,
/// 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 {
fn from(e: PodSliceError) -> Self {
ProgramError::Custom(e as u32)
}
}

impl ToStr for PodSliceError {
fn to_str(&self) -> &'static str {
match self {
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
}
}
13 changes: 6 additions & 7 deletions pod/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#![no_std]

//! Crate containing `Pod` types and `bytemuck` utilities used in SPL

#[cfg(any(feature = "borsh", feature = "serde", test))]
extern crate alloc;

#[cfg(feature = "bytemuck")]
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;

// Export current sdk types for downstream users building with a different sdk
// version
pub use {solana_address, solana_program_error, solana_program_option};
pub use {solana_program_error, solana_program_option, solana_pubkey};
28 changes: 28 additions & 0 deletions pod/src/list/list_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use {
crate::{list::ListView, pod_length::PodLength},
bytemuck::Pod,
solana_program_error::ProgramError,
std::ops::Deref,
};

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

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

/// 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
Loading