diff --git a/Cargo.lock b/Cargo.lock index 96ca0cf0..f99d91ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1527,6 +1527,16 @@ dependencies = [ "solana-sanitize", ] +[[package]] +name = "solana-zero-copy" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94f52dd8f733a13f6a18e55de83cf97c4c3f5fdf27ea3830bcff0b35313efcc2" +dependencies = [ + "bytemuck", + "bytemuck_derive", +] + [[package]] name = "solana-zk-sdk" version = "4.0.0" @@ -1672,7 +1682,7 @@ dependencies = [ "num-traits", "num_enum", "solana-program-error", - "spl-pod 0.7.2", + "solana-zero-copy", "thiserror 2.0.18", ] diff --git a/list-view/Cargo.toml b/list-view/Cargo.toml index 33e55d5e..a3172f76 100644 --- a/list-view/Cargo.toml +++ b/list-view/Cargo.toml @@ -13,7 +13,7 @@ num-derive = "0.4.2" num_enum = "0.7.5" num-traits = "0.2.19" solana-program-error = "3.0.0" -spl-pod = { version = "0.7.2", path = "../pod", features = ["bytemuck"] } +solana-zero-copy = { version = "1.0.0", features = ["bytemuck"] } thiserror = "2.0.18" [dev-dependencies] diff --git a/list-view/src/list_trait.rs b/list-view/src/list_trait.rs index b9265c24..05f2d8f1 100644 --- a/list-view/src/list_trait.rs +++ b/list-view/src/list_trait.rs @@ -10,7 +10,7 @@ use { pub trait List: Deref { /// The type of the items stored in the list. type Item: Pod; - /// Length prefix type used (`PodU16`, `PodU32`, …). + /// Length prefix type used (`U16`, `U32`, ...). type Length: PodLength; /// Returns the total number of items that can be stored in the list. diff --git a/list-view/src/list_view.rs b/list-view/src/list_view.rs index aef4fdc9..0c811517 100644 --- a/list-view/src/list_view.rs +++ b/list-view/src/list_view.rs @@ -5,19 +5,14 @@ use { error::ListViewError, list_view_mut::ListViewMut, list_view_read_only::ListViewReadOnly, pod_length::PodLength, }, - bytemuck::Pod, + bytemuck::{try_cast_slice, try_cast_slice_mut, try_from_bytes, try_from_bytes_mut, Pod}, core::{ marker::PhantomData, mem::{align_of, size_of}, ops::Range, }, solana_program_error::ProgramError, - spl_pod::{ - bytemuck::{ - pod_from_bytes, pod_from_bytes_mut, pod_slice_from_bytes, pod_slice_from_bytes_mut, - }, - primitives::PodU32, - }, + solana_zero_copy::unaligned::U32, }; /// An API for interpreting a raw buffer (`&[u8]`) as a variable-length collection of Pod elements. @@ -36,13 +31,13 @@ use { /// The structure assumes the underlying byte buffer is formatted as follows: /// 1. **Length**: A length field of type `L` at the beginning of the buffer, /// indicating the number of currently active elements in the collection. -/// Defaults to `PodU32`. The implementation uses padding to ensure that the +/// Defaults to `U32`. The implementation uses padding to ensure that the /// data is correctly aligned for any `Pod` type. /// 2. **Padding**: Optional padding bytes to ensure proper alignment of the data. /// 3. **Data**: The remaining part of the buffer, which is treated as a slice /// of `T` elements. The capacity of the collection is the number of `T` /// elements that can fit into this data portion. -pub struct ListView(PhantomData<(T, L)>); +pub struct ListView(PhantomData<(T, L)>); struct Layout { length_range: Range, @@ -75,8 +70,9 @@ impl ListView { let len_bytes = &buf[layout.length_range]; let data_bytes = &buf[layout.data_range]; - let length = pod_from_bytes::(len_bytes)?; - let data = pod_slice_from_bytes::(data_bytes)?; + let length = try_from_bytes::(len_bytes).map_err(|_| ProgramError::InvalidArgument)?; + let data = + try_cast_slice::(data_bytes).map_err(|_| ProgramError::InvalidArgument)?; let capacity = data.len(); if (*length).into() > capacity { @@ -121,8 +117,10 @@ impl ListView { let len_bytes = &mut header_bytes[layout.length_range]; // Cast the bytes to typed data - let length = pod_from_bytes_mut::(len_bytes)?; - let data = pod_slice_from_bytes_mut::(data_bytes)?; + let length = + try_from_bytes_mut::(len_bytes).map_err(|_| ProgramError::InvalidArgument)?; + let data = + try_cast_slice_mut::(data_bytes).map_err(|_| ProgramError::InvalidArgument)?; let capacity = data.len(); Ok(ListViewMut { @@ -188,7 +186,9 @@ mod tests { super::*, crate::List, bytemuck_derive::{Pod as DerivePod, Zeroable}, - spl_pod::primitives::{PodU128, PodU16, PodU32, PodU64}, + solana_zero_copy::unaligned::{ + U128 as PodU128, U16 as PodU16, U32 as PodU32, U64 as PodU64, + }, }; #[test] diff --git a/list-view/src/list_view_mut.rs b/list-view/src/list_view_mut.rs index 210ccba2..ba201711 100644 --- a/list-view/src/list_view_mut.rs +++ b/list-view/src/list_view_mut.rs @@ -5,11 +5,11 @@ use { bytemuck::Pod, core::ops::{Deref, DerefMut}, solana_program_error::ProgramError, - spl_pod::primitives::PodU32, + solana_zero_copy::unaligned::U32, }; #[derive(Debug)] -pub struct ListViewMut<'data, T: Pod, L: PodLength = PodU32> { +pub struct ListViewMut<'data, T: Pod, L: PodLength = U32> { pub(crate) length: &'data mut L, pub(crate) data: &'data mut [T], pub(crate) capacity: usize, @@ -83,7 +83,7 @@ mod tests { super::*, crate::{List, ListView}, bytemuck_derive::{Pod, Zeroable}, - spl_pod::primitives::{PodU16, PodU32, PodU64}, + solana_zero_copy::unaligned::{U16 as PodU16, U32 as PodU32, U64 as PodU64}, }; #[repr(C)] diff --git a/list-view/src/list_view_read_only.rs b/list-view/src/list_view_read_only.rs index 122d2224..05dd9d4f 100644 --- a/list-view/src/list_view_read_only.rs +++ b/list-view/src/list_view_read_only.rs @@ -4,11 +4,11 @@ use { crate::{list_trait::List, pod_length::PodLength}, bytemuck::Pod, core::ops::Deref, - spl_pod::primitives::PodU32, + solana_zero_copy::unaligned::U32, }; #[derive(Debug)] -pub struct ListViewReadOnly<'data, T: Pod, L: PodLength = PodU32> { +pub struct ListViewReadOnly<'data, T: Pod, L: PodLength = U32> { pub(crate) length: &'data L, pub(crate) data: &'data [T], pub(crate) capacity: usize, @@ -39,7 +39,7 @@ mod tests { crate::ListView, bytemuck_derive::{Pod as DerivePod, Zeroable}, core::mem::size_of, - spl_pod::primitives::{PodU32, PodU64}, + solana_zero_copy::unaligned::{U32 as PodU32, U64 as PodU64}, }; #[repr(C, align(16))] diff --git a/tlv-account-resolution/src/state.rs b/tlv-account-resolution/src/state.rs index cd0dcfb8..37874c8c 100644 --- a/tlv-account-resolution/src/state.rs +++ b/tlv-account-resolution/src/state.rs @@ -191,7 +191,7 @@ impl ExtraAccountMetaList { tlv_state: &'a TlvStateBorrowed, ) -> Result, ProgramError> { let bytes = tlv_state.get_first_bytes::()?; - ListView::::unpack(bytes) + ListView::::unpack(bytes) } /// Get the byte size required to hold `num_items` items