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
12 changes: 11 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion list-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion list-view/src/list_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
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`, ).
/// Length prefix type used (`U16`, `U32`, ...).
type Length: PodLength;

/// Returns the total number of items that can be stored in the list.
Expand Down
28 changes: 14 additions & 14 deletions list-view/src/list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<T: Pod, L: PodLength = PodU32>(PhantomData<(T, L)>);
pub struct ListView<T: Pod, L: PodLength = U32>(PhantomData<(T, L)>);

struct Layout {
length_range: Range<usize>,
Expand Down Expand Up @@ -75,8 +70,9 @@ impl<T: Pod, L: PodLength> ListView<T, L> {
let len_bytes = &buf[layout.length_range];
let data_bytes = &buf[layout.data_range];

let length = pod_from_bytes::<L>(len_bytes)?;
let data = pod_slice_from_bytes::<T>(data_bytes)?;
let length = try_from_bytes::<L>(len_bytes).map_err(|_| ProgramError::InvalidArgument)?;
let data =
try_cast_slice::<u8, T>(data_bytes).map_err(|_| ProgramError::InvalidArgument)?;
let capacity = data.len();

if (*length).into() > capacity {
Expand Down Expand Up @@ -121,8 +117,10 @@ impl<T: Pod, L: PodLength> ListView<T, L> {
let len_bytes = &mut header_bytes[layout.length_range];

// Cast the bytes to typed data
let length = pod_from_bytes_mut::<L>(len_bytes)?;
let data = pod_slice_from_bytes_mut::<T>(data_bytes)?;
let length =
try_from_bytes_mut::<L>(len_bytes).map_err(|_| ProgramError::InvalidArgument)?;
let data =
try_cast_slice_mut::<u8, T>(data_bytes).map_err(|_| ProgramError::InvalidArgument)?;
let capacity = data.len();

Ok(ListViewMut {
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions list-view/src/list_view_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions list-view/src/list_view_read_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))]
Expand Down
2 changes: 1 addition & 1 deletion tlv-account-resolution/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl ExtraAccountMetaList {
tlv_state: &'a TlvStateBorrowed,
) -> Result<ListViewReadOnly<'a, ExtraAccountMeta, PodU32>, ProgramError> {
let bytes = tlv_state.get_first_bytes::<T>()?;
ListView::<ExtraAccountMeta>::unpack(bytes)
ListView::<ExtraAccountMeta, PodU32>::unpack(bytes)
}

/// Get the byte size required to hold `num_items` items
Expand Down
Loading