-
Notifications
You must be signed in to change notification settings - Fork 17
Alias to new solana-zero-copy types #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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> { | ||
|
|
@@ -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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. init needs the |
||
| { | ||
| /// 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}, | ||
| }; | ||
|
|
@@ -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); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test helper has to spell out |
||
| let size = ListView::<T, L>::size_of(capacity).unwrap(); | ||
| let mut buffer = vec![0u8; size]; | ||
|
|
||
|
|
||
| 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); |
There was a problem hiding this comment.
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 😅