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
66 changes: 16 additions & 50 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions pod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ license = "Apache-2.0"
edition = "2021"

[features]
serde-traits = ["dep:serde"]
borsh = ["dep:borsh", "solana-pubkey/borsh"]
wincode = ["dep:wincode"]
serde-traits = ["dep:serde", "solana-zero-copy/serde"]
borsh = ["dep:borsh", "solana-pubkey/borsh", "solana-zero-copy/borsh"]
wincode = ["dep:wincode", "solana-zero-copy/wincode"]

[dependencies]
borsh = { version = "1.5.7", features = ["derive", "unstable__schema"], optional = true }
Expand All @@ -24,6 +24,7 @@ wincode = { version = "0.4.4", features = ["derive"], optional = true }
solana-program-error = "3.0.0"
solana-program-option = "3.0.0"
solana-pubkey = "3.0.0"
solana-zero-copy = { version = "1.0.0", features = ["bytemuck"] }
solana-zk-sdk = "4.0.0"
thiserror = "2.0"

Expand Down
3 changes: 3 additions & 0 deletions pod/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub mod pod_length;
pub mod primitives;
pub mod slice;

// Re-export the conversion macro (replaces the old #[macro_export] definition)
pub use solana_zero_copy::impl_int_conversion;
Comment on lines +12 to +13
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we export that macro 😅


// Export current sdk types for downstream users building with a different sdk
// version
pub use {solana_program_error, solana_program_option, solana_pubkey};
25 changes: 17 additions & 8 deletions pod/src/list/list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,6 @@ impl<T: Pod, L: PodLength> ListView<T, L> {
Ok(view)
}

/// Initialize a buffer: sets `length = 0` and returns a mutable `ListViewMut`.
pub fn init(buf: &mut [u8]) -> Result<ListViewMut<T, L>, ProgramError> {
let view = Self::build_mut_view(buf)?;
*view.length = L::try_from(0)?;
Ok(view)
}

/// Internal helper to build a mutable view without validation or initialization.
#[inline]
fn build_mut_view(buf: &mut [u8]) -> Result<ListViewMut<T, L>, ProgramError> {
Expand Down Expand Up @@ -181,13 +174,28 @@ impl<T: Pod, L: PodLength> ListView<T, L> {
}
}

impl<T: Pod, L> ListView<T, L>
where
L: PodLength,
PodSliceError: From<<L as TryFrom<usize>>::Error>,
Comment on lines +178 to +180
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init needs the where to preserve the old length-conversion behavior

{
/// Initialize a buffer: sets `length = 0` and returns a mutable `ListViewMut`.
pub fn init(buf: &mut [u8]) -> Result<ListViewMut<T, L>, ProgramError> {
let view = Self::build_mut_view(buf)?;
*view.length = L::try_from(0).map_err(PodSliceError::from)?;
Ok(view)
}
}

#[cfg(test)]
mod tests {
#[cfg(not(target_arch = "bpf"))]
use crate::primitives::PodU128;
use {
super::*,
crate::{
list::List,
primitives::{PodU128, PodU16, PodU32, PodU64},
primitives::{PodU16, PodU32, PodU64},
},
bytemuck_derive::{Pod as DerivePod, Zeroable},
};
Expand Down Expand Up @@ -641,5 +649,6 @@ mod tests {
test_list_view_for_length_type!(list_view_with_pod_u16, PodU16);
test_list_view_for_length_type!(list_view_with_pod_u32, PodU32);
test_list_view_for_length_type!(list_view_with_pod_u64, PodU64);
#[cfg(not(target_arch = "bpf"))]
test_list_view_for_length_type!(list_view_with_pod_u128, PodU128);
}
15 changes: 11 additions & 4 deletions pod/src/list/list_view_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ pub struct ListViewMut<'data, T: Pod, L: PodLength = PodU32> {
pub(crate) capacity: usize,
}

impl<T: Pod, L: PodLength> ListViewMut<'_, T, L> {
impl<T: Pod, L> ListViewMut<'_, T, L>
where
L: PodLength,
PodSliceError: From<<L as TryFrom<usize>>::Error>,
{
/// Add another item to the slice
pub fn push(&mut self, item: T) -> Result<(), ProgramError> {
let length = (*self.length).into();
if length >= self.capacity {
Err(PodSliceError::BufferTooSmall.into())
} else {
self.data[length] = item;
*self.length = L::try_from(length.saturating_add(1))?;
*self.length = L::try_from(length.saturating_add(1)).map_err(PodSliceError::from)?;
Ok(())
}
}
Expand All @@ -47,7 +51,7 @@ impl<T: Pod, L: PodLength> ListViewMut<'_, T, L> {

// Store the new length (len - 1)
let new_len = len.checked_sub(1).unwrap();
*self.length = L::try_from(new_len)?;
*self.length = L::try_from(new_len).map_err(PodSliceError::from)?;

Ok(removed_item)
}
Expand Down Expand Up @@ -110,7 +114,10 @@ mod tests {
fn init_view_mut<T: Pod, L: PodLength>(
buffer: &mut Vec<u8>,
capacity: usize,
) -> ListViewMut<T, L> {
) -> ListViewMut<T, L>
where
PodSliceError: From<<L as TryFrom<usize>>::Error>,
{
let size = ListView::<T, L>::size_of(capacity).unwrap();
buffer.resize(size, 0);
ListView::<T, L>::init(buffer).unwrap()
Expand Down
5 changes: 4 additions & 1 deletion pod/src/list/list_view_read_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ mod tests {
length: usize,
capacity: usize,
items: &[T],
) -> Vec<u8> {
) -> Vec<u8>
where
<L as TryFrom<usize>>::Error: std::fmt::Debug,
{
Comment on lines +57 to +59
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test helper has to spell out Debug now because unwrap() can no longer assume the conversion error is the old concrete type

let size = ListView::<T, L>::size_of(capacity).unwrap();
let mut buffer = vec![0u8; size];

Expand Down
42 changes: 7 additions & 35 deletions pod/src/pod_length.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
use {
crate::{
error::PodSliceError,
primitives::{PodU128, PodU16, PodU32, PodU64},
},
bytemuck::Pod,
};
use {crate::error::PodSliceError, bytemuck::Pod};

/// Marker trait for converting to/from Pod `uint`'s and `usize`
pub trait PodLength: Pod + Into<usize> + TryFrom<usize, Error = PodSliceError> {}
pub trait PodLength: Pod + Into<usize> + TryFrom<usize> {}

/// Blanket implementation to automatically implement `PodLength` for any type
/// that satisfies the required bounds.
impl<T> PodLength for T where T: Pod + Into<usize> + TryFrom<usize, Error = PodSliceError> {}

/// Implements the `TryFrom<usize>` and `From<T> for usize` conversions for a Pod integer type
macro_rules! impl_pod_length_for {
($PodType:ty, $PrimitiveType:ty) => {
impl TryFrom<usize> for $PodType {
type Error = PodSliceError;

fn try_from(val: usize) -> Result<Self, Self::Error> {
let primitive_val = <$PrimitiveType>::try_from(val)?;
Ok(primitive_val.into())
}
}

impl From<$PodType> for usize {
fn from(pod_val: $PodType) -> Self {
let primitive_val = <$PrimitiveType>::from(pod_val);
Self::try_from(primitive_val)
.expect("value out of range for usize on this platform")
}
}
};
impl<T> PodLength for T
where
T: Pod + Into<usize> + TryFrom<usize>,
PodSliceError: From<<T as TryFrom<usize>>::Error>,
{
}

impl_pod_length_for!(PodU16, u16);
impl_pod_length_for!(PodU32, u32);
impl_pod_length_for!(PodU64, u64);
impl_pod_length_for!(PodU128, u128);
Loading
Loading