From 0ed92f0b12ff0ea938201c2bdd33f1d24e621f3a Mon Sep 17 00:00:00 2001 From: ananas-block Date: Sat, 4 Jan 2025 23:41:50 +0000 Subject: [PATCH] chore: clean up trait bounds use FromPrimitive + ToPrimitive + PrimInt --- program-libs/zero-copy/Cargo.toml | 1 + program-libs/zero-copy/src/cyclic_vec.rs | 112 +++++------------- program-libs/zero-copy/src/slice_mut.rs | 78 +++++------- program-libs/zero-copy/src/vec.rs | 78 +++++------- program-libs/zero-copy/src/wrapped_pointer.rs | 6 +- .../zero-copy/src/wrapped_pointer_mut.rs | 8 +- .../zero-copy/tests/slice_mut_test.rs | 56 ++++----- program-libs/zero-copy/tests/vec_tests.rs | 50 ++------ 8 files changed, 132 insertions(+), 257 deletions(-) diff --git a/program-libs/zero-copy/Cargo.toml b/program-libs/zero-copy/Cargo.toml index 8d6b2ac75..0cc307801 100644 --- a/program-libs/zero-copy/Cargo.toml +++ b/program-libs/zero-copy/Cargo.toml @@ -13,6 +13,7 @@ solana = ["solana-program"] [dependencies] solana-program = { workspace = true, optional = true } thiserror = "1.0" +num-traits = { version = "0.2" } [dev-dependencies] rand = "0.8" diff --git a/program-libs/zero-copy/src/cyclic_vec.rs b/program-libs/zero-copy/src/cyclic_vec.rs index f0e4917cd..3911c17dd 100644 --- a/program-libs/zero-copy/src/cyclic_vec.rs +++ b/program-libs/zero-copy/src/cyclic_vec.rs @@ -3,9 +3,11 @@ use std::{ fmt::Debug, marker::PhantomData, mem::size_of, - ops::{Add, Index, IndexMut, Rem}, + ops::{Index, IndexMut}, }; +use num_traits::{FromPrimitive, PrimInt, ToPrimitive}; + use crate::{ add_padding, errors::ZeroCopyError, vec::ZeroCopyVec, wrapped_pointer_mut::WrappedPointerMut, }; @@ -18,10 +20,8 @@ pub type ZeroCopyCyclicVecU8 = ZeroCopyCyclicVec; pub struct ZeroCopyCyclicVec where - LEN: TryFrom + Clone + Copy + TryInto + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { current_index: WrappedPointerMut, vec: ZeroCopyVec, @@ -29,15 +29,8 @@ where impl ZeroCopyCyclicVec where - LEN: TryFrom - + TryInto - + Copy - + Clone - + Add - + Rem, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { pub fn new(capacity: LEN, vec: &mut [u8]) -> Result { Self::new_at(capacity, vec, &mut 0) @@ -48,8 +41,7 @@ where vec: &mut [u8], offset: &mut usize, ) -> Result { - let current_index = - WrappedPointerMut::::new_at(LEN::try_from(0).unwrap(), vec, offset)?; + let current_index = WrappedPointerMut::::new_at(LEN::zero(), vec, offset)?; add_padding::(offset); let vec = ZeroCopyVec::::new_at(capacity, vec, offset)?; Ok(Self { current_index, vec }) @@ -72,15 +64,8 @@ where impl ZeroCopyCyclicVec where - LEN: TryFrom - + TryInto - + Copy - + Clone - + Add - + Rem, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { pub fn from_bytes(account_data: &mut [u8]) -> Result { Self::from_bytes_at(account_data, &mut 0) @@ -112,15 +97,8 @@ where impl ZeroCopyCyclicVec where - LEN: TryFrom - + TryInto - + Copy - + Clone - + Add - + Rem, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { #[inline] pub fn push(&mut self, value: T) { @@ -131,12 +109,12 @@ where self.vec[current_index] = value; } let new_index = (self.current_index() + 1) % self.vec.capacity(); - *self.current_index = LEN::try_from(new_index).unwrap(); + *self.current_index = LEN::from_usize(new_index).unwrap(); } #[inline] pub fn clear(&mut self) { - *self.current_index = 0.try_into().unwrap(); + *self.current_index = LEN::zero(); self.vec.clear(); } @@ -162,7 +140,7 @@ where #[inline] fn current_index(&self) -> usize { - (*self.current_index.get()).try_into().unwrap() + (*self.current_index.get()).to_usize().unwrap() } /// First index is the next index after the last index mod capacity. @@ -217,11 +195,11 @@ where } pub fn data_size(length: LEN) -> usize { - ZeroCopyVec::::required_size_for_capacity(length.try_into().unwrap()) + ZeroCopyVec::::required_size_for_capacity(length.to_usize().unwrap()) } pub fn required_size_for_capacity(capacity: usize) -> usize { - Self::metadata_size() + Self::data_size(capacity.try_into().unwrap()) + Self::metadata_size() + Self::data_size(LEN::from_usize(capacity).unwrap()) } #[inline] @@ -271,10 +249,8 @@ where pub struct ZeroCopyCyclicVecIterator<'a, LEN, T> where - LEN: TryFrom + Clone + Copy + TryInto + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { vec: &'a ZeroCopyCyclicVec, current: usize, @@ -284,15 +260,8 @@ where impl<'a, LEN, T> Iterator for ZeroCopyCyclicVecIterator<'a, LEN, T> where - LEN: TryFrom - + TryInto - + Copy - + Clone - + Rem - + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { type Item = &'a T; @@ -314,15 +283,8 @@ where impl IndexMut for ZeroCopyCyclicVec where - LEN: TryFrom - + TryInto - + Copy - + Clone - + Add - + Rem, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { #[inline] fn index_mut(&mut self, index: usize) -> &mut Self::Output { @@ -333,15 +295,8 @@ where impl Index for ZeroCopyCyclicVec where - LEN: TryFrom - + TryInto - + Copy - + Clone - + Add - + Rem, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { type Output = T; @@ -354,10 +309,8 @@ where impl PartialEq for ZeroCopyCyclicVec where - LEN: TryFrom + TryInto + Copy + Clone + Add + PartialEq, - T: Copy + Clone + PartialEq, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy + PartialEq, { #[inline] fn eq(&self, other: &Self) -> bool { @@ -367,15 +320,8 @@ where impl fmt::Debug for ZeroCopyCyclicVec where - LEN: TryFrom - + TryInto - + Copy - + Clone - + Add - + Rem, - T: Copy + Clone + Debug, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy + Debug, { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/program-libs/zero-copy/src/slice_mut.rs b/program-libs/zero-copy/src/slice_mut.rs index c36bba58a..1eb6a57b9 100644 --- a/program-libs/zero-copy/src/slice_mut.rs +++ b/program-libs/zero-copy/src/slice_mut.rs @@ -2,7 +2,7 @@ use core::{fmt, slice}; use std::{ marker::PhantomData, mem::{size_of, ManuallyDrop}, - ops::{Add, Index, IndexMut}, + ops::{Index, IndexMut}, }; use crate::{add_padding, check_alignment, errors::ZeroCopyError, wrapped_pointer::WrappedPointer}; @@ -16,19 +16,17 @@ pub type ZeroCopySliceMutU8 = ZeroCopySliceMut; #[repr(C)] pub struct ZeroCopySliceMut where - LEN: Copy + Clone, + LEN: Copy, { length: WrappedPointer, data: ManuallyDrop<*mut T>, _marker: PhantomData, } - +use num_traits::{FromPrimitive, PrimInt, ToPrimitive}; impl ZeroCopySliceMut where - LEN: TryFrom + TryInto + Copy + Clone + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: ToPrimitive + Copy, + T: Copy, { pub fn new(length: LEN, data: &mut [u8]) -> Result { Self::new_at(length, data, &mut 0) @@ -36,10 +34,10 @@ where pub fn new_at(length: LEN, data: &mut [u8], offset: &mut usize) -> Result { let data = data.split_at_mut(*offset).1; - if Self::required_size_for_capacity(length.try_into().unwrap()) > data.len() { + if Self::required_size_for_capacity(length) > data.len() { return Err(ZeroCopyError::InsufficientMemoryAllocated( data.len(), - Self::required_size_for_capacity(length.try_into().unwrap()), + Self::required_size_for_capacity(length), )); } @@ -141,21 +139,19 @@ where #[inline] pub fn data_size(length: LEN) -> usize { - length.try_into().unwrap() * size_of::() + length.to_usize().unwrap() * size_of::() } #[inline] - pub fn required_size_for_capacity(capacity: usize) -> usize { - Self::metadata_size() + Self::data_size(capacity.try_into().unwrap()) + pub fn required_size_for_capacity(capacity: LEN) -> usize { + Self::metadata_size() + Self::data_size(capacity) } } impl ZeroCopySliceMut where - LEN: TryFrom + TryInto + Copy + Clone + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: ToPrimitive + Copy, + T: Copy, { pub fn copy_from_slice(&mut self, slice: &[T]) { let len = slice.len(); @@ -175,14 +171,12 @@ where impl ZeroCopySliceMut where - LEN: TryFrom + TryInto + Copy, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: ToPrimitive + Copy, + T: Copy, { #[inline] pub fn len(&self) -> usize { - (*self.length).try_into().unwrap() + (*self.length).to_usize().unwrap() } #[inline] @@ -248,10 +242,8 @@ where impl IndexMut for ZeroCopySliceMut where - LEN: TryFrom + TryInto + Copy + Clone + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + Copy, + T: Copy, { #[inline] fn index_mut(&mut self, index: usize) -> &mut Self::Output { @@ -261,10 +253,8 @@ where impl Index for ZeroCopySliceMut where - LEN: TryFrom + TryInto + Copy + Clone + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + Copy, + T: Copy, { type Output = T; @@ -276,10 +266,8 @@ where impl<'a, LEN, T> IntoIterator for &'a ZeroCopySliceMut where - LEN: Copy + Clone + TryFrom + TryInto + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { type Item = &'a T; type IntoIter = slice::Iter<'a, T>; @@ -292,10 +280,8 @@ where impl<'a, LEN, T> IntoIterator for &'a mut ZeroCopySliceMut where - LEN: Copy + Clone + TryFrom + TryInto + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; @@ -308,10 +294,8 @@ where impl<'b, LEN, T> ZeroCopySliceMut where - LEN: Copy + Clone + TryFrom + TryInto + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { #[inline] pub fn iter(&'b self) -> slice::Iter<'b, T> { @@ -326,10 +310,8 @@ where impl PartialEq for ZeroCopySliceMut where - LEN: TryFrom + TryInto + Copy, - T: Copy + PartialEq + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + Copy, + T: Copy + PartialEq, { #[inline] fn eq(&self, other: &Self) -> bool { @@ -339,10 +321,8 @@ where impl fmt::Debug for ZeroCopySliceMut where - T: Copy + Clone + fmt::Debug, - LEN: TryFrom + TryInto + Copy + Clone + Add, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + T: Copy + fmt::Debug, + LEN: FromPrimitive + ToPrimitive + Copy, { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/program-libs/zero-copy/src/vec.rs b/program-libs/zero-copy/src/vec.rs index 3229e203b..0c44b0fe3 100644 --- a/program-libs/zero-copy/src/vec.rs +++ b/program-libs/zero-copy/src/vec.rs @@ -2,10 +2,12 @@ use core::slice; use std::{ fmt, mem::size_of, - ops::{Add, Index, IndexMut}, + ops::{Index, IndexMut}, ptr::{self}, }; +use num_traits::{FromPrimitive, PrimInt, ToPrimitive}; + use crate::{ add_padding, errors::ZeroCopyError, slice_mut::ZeroCopySliceMut, wrapped_pointer_mut::WrappedPointerMut, @@ -23,9 +25,8 @@ pub type ZeroCopyVecU8 = ZeroCopyVec; /// (that makes it different from [`Vec`](std::vec::Vec)). pub struct ZeroCopyVec where - LEN: TryFrom + Clone + Copy, - T: Copy + Clone, - >::Error: fmt::Debug, + LEN: FromPrimitive + Copy, + T: Copy, { length: WrappedPointerMut, data: ZeroCopySliceMut, @@ -33,10 +34,8 @@ where impl ZeroCopyVec where - LEN: TryFrom + TryInto + Copy + Clone + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { pub fn new(capacity: LEN, data: &mut [u8]) -> Result { Self::new_at(capacity, data, &mut 0) @@ -47,8 +46,7 @@ where data: &mut [u8], offset: &mut usize, ) -> Result { - let length = - WrappedPointerMut::::new_at(LEN::try_from(0).unwrap(), data, offset).unwrap(); + let length = WrappedPointerMut::::new_at(LEN::zero(), data, offset).unwrap(); add_padding::(offset); let data = ZeroCopySliceMut::::new_at(capacity, data, offset)?; Ok(Self { length, data }) @@ -107,14 +105,14 @@ where } unsafe { ptr::write(self.data.data_as_mut_ptr().add(self.len()), value) }; - *self.length = *self.length + LEN::try_from(1).unwrap(); + *self.length = *self.length + LEN::one(); Ok(()) } #[inline] pub fn clear(&mut self) { - *self.length.get_mut() = 0.try_into().unwrap(); + *self.length.get_mut() = LEN::zero(); } #[inline] @@ -126,25 +124,23 @@ where #[inline] pub fn data_size(length: LEN) -> usize { - ZeroCopySliceMut::::required_size_for_capacity(length.try_into().unwrap()) + ZeroCopySliceMut::::required_size_for_capacity(length) } #[inline] pub fn required_size_for_capacity(capacity: usize) -> usize { - Self::metadata_size() + Self::data_size(capacity.try_into().unwrap()) + Self::metadata_size() + Self::data_size(LEN::from_usize(capacity).unwrap()) } } impl ZeroCopyVec where - LEN: TryFrom + TryInto + Copy + Clone + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { #[inline] pub fn len(&self) -> usize { - (*self.length).try_into().unwrap() + (*self.length).to_usize().unwrap() } #[inline] @@ -206,7 +202,7 @@ where panic!("Capacity overflow. Cannot copy slice into ZeroCopyVec"); } self.data.as_mut_slice()[len..].copy_from_slice(slice); - *self.length = LEN::try_from(new_len).unwrap(); + *self.length = LEN::from_usize(new_len).unwrap(); } #[inline] @@ -221,10 +217,8 @@ where impl IndexMut for ZeroCopyVec where - LEN: TryFrom + TryInto + Copy + Clone + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { #[inline] fn index_mut(&mut self, index: usize) -> &mut Self::Output { @@ -235,10 +229,8 @@ where impl Index for ZeroCopyVec where - LEN: TryFrom + TryInto + Copy + Clone + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { type Output = T; @@ -251,10 +243,8 @@ where impl<'a, LEN, T> IntoIterator for &'a ZeroCopyVec where - LEN: Copy + Clone + TryFrom + TryInto + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { type Item = &'a T; type IntoIter = slice::Iter<'a, T>; @@ -267,10 +257,8 @@ where impl<'a, LEN, T> IntoIterator for &'a mut ZeroCopyVec where - LEN: Copy + Clone + TryFrom + TryInto + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; @@ -283,10 +271,8 @@ where impl<'b, LEN, T> ZeroCopyVec where - LEN: Copy + Clone + TryFrom + TryInto + Add, - T: Copy + Clone, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy, { #[inline] pub fn iter(&'b self) -> slice::Iter<'b, T> { @@ -301,10 +287,8 @@ where impl PartialEq for ZeroCopyVec where - LEN: TryFrom + TryInto + Copy + Clone + Add, - T: Copy + Clone + PartialEq, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy + PartialEq, { #[inline] fn eq(&self, other: &Self) -> bool { @@ -314,10 +298,8 @@ where impl fmt::Debug for ZeroCopyVec where - T: Copy + Clone + fmt::Debug, - LEN: TryFrom + TryInto + Copy + Clone + Add, - >::Error: fmt::Debug, - >::Error: fmt::Debug, + LEN: FromPrimitive + ToPrimitive + PrimInt, + T: Copy + fmt::Debug, { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/program-libs/zero-copy/src/wrapped_pointer.rs b/program-libs/zero-copy/src/wrapped_pointer.rs index 3a9e95d0e..c61be5ca4 100644 --- a/program-libs/zero-copy/src/wrapped_pointer.rs +++ b/program-libs/zero-copy/src/wrapped_pointer.rs @@ -10,7 +10,7 @@ use crate::{check_alignment, check_size, errors::ZeroCopyError}; pub struct WrappedPointer where - T: Copy + Clone, + T: Copy, { ptr: ManuallyDrop<*const T>, _marker: PhantomData, @@ -18,7 +18,7 @@ where impl WrappedPointer where - T: Copy + Clone, + T: Copy, { pub fn new(value: T, data: &mut [u8]) -> Result { check_size::(data)?; @@ -100,7 +100,7 @@ where impl Deref for WrappedPointer where - T: Copy + Clone, + T: Copy, { type Target = T; diff --git a/program-libs/zero-copy/src/wrapped_pointer_mut.rs b/program-libs/zero-copy/src/wrapped_pointer_mut.rs index 9cbee7437..5fabeed0e 100644 --- a/program-libs/zero-copy/src/wrapped_pointer_mut.rs +++ b/program-libs/zero-copy/src/wrapped_pointer_mut.rs @@ -10,7 +10,7 @@ use crate::{check_alignment, check_size, errors::ZeroCopyError}; pub struct WrappedPointerMut where - T: Copy + Clone, + T: Copy, { ptr: ManuallyDrop<*mut T>, _marker: PhantomData, @@ -18,7 +18,7 @@ where impl WrappedPointerMut where - T: Copy + Clone, + T: Copy, { pub fn new(value: T, data: &mut [u8]) -> Result { check_size::(data)?; @@ -107,7 +107,7 @@ where impl Deref for WrappedPointerMut where - T: Copy + Clone, + T: Copy, { type Target = T; @@ -118,7 +118,7 @@ where impl DerefMut for WrappedPointerMut where - T: Copy + Clone, + T: Copy, { fn deref_mut(&mut self) -> &mut Self::Target { self.get_mut() diff --git a/program-libs/zero-copy/tests/slice_mut_test.rs b/program-libs/zero-copy/tests/slice_mut_test.rs index 5d787894b..5ed5c8d78 100644 --- a/program-libs/zero-copy/tests/slice_mut_test.rs +++ b/program-libs/zero-copy/tests/slice_mut_test.rs @@ -1,36 +1,26 @@ use core::fmt::Debug; -use std::{convert::TryInto, ops::Add}; use light_zero_copy::{ add_padding, errors::ZeroCopyError, slice_mut::{ZeroCopySliceMut, ZeroCopySliceMutUsize}, }; +use num_traits::{FromPrimitive, PrimInt, ToPrimitive}; use rand::{distributions::Standard, prelude::*}; fn test_zero_copy_slice_mut_new(length: LEN) where - LEN: Debug - + Copy - + Clone - + Add - + TryInto - + TryFrom - + PartialOrd - + num_traits::ToBytes, - >::Error: std::fmt::Debug, - >::Error: std::fmt::Debug, + LEN: ToPrimitive + FromPrimitive + PrimInt + num_traits::ToBytes, T: Copy + Clone + PartialEq + Debug + Default, Standard: Distribution, { let mut rng = thread_rng(); - let mut data = - vec![0; ZeroCopySliceMut::::required_size_for_capacity(length.try_into().unwrap())]; + let mut data = vec![0; ZeroCopySliceMut::::required_size_for_capacity(length)]; ZeroCopySliceMut::::new(length, &mut data).unwrap(); // Test from_bytes with a zeroed slice { - let reference_vec = vec![T::default(); length.try_into().unwrap()]; + let reference_vec = vec![T::default(); length.to_usize().unwrap()]; let slice = ZeroCopySliceMut::::from_bytes(&mut data).unwrap(); // 1. Validate length assert_eq!(slice.len(), reference_vec.len()); @@ -59,7 +49,7 @@ where assert_eq!(T::default(), *slice.last().unwrap()); } - let length_usize: usize = length.try_into().unwrap(); + let length_usize: usize = length.to_usize().unwrap(); let mut reference_vec = vec![T::default(); length_usize]; // Fill the slice completely and verify properties @@ -179,7 +169,7 @@ fn test_empty() { let mut data = vec![0; ZeroCopySliceMut::::required_size_for_capacity(length)]; let mut zero_copy_slice = ZeroCopySliceMut::::new(u8::try_from(length).unwrap(), &mut data).unwrap(); - assert_eq!(zero_copy_slice.len(), length.try_into().unwrap()); + assert_eq!(zero_copy_slice.len(), length.to_usize().unwrap()); assert!(zero_copy_slice.is_empty()); assert_eq!(zero_copy_slice.first(), None); assert_eq!(zero_copy_slice.last(), None); @@ -201,7 +191,7 @@ fn test_index_out_of_bounds() { let mut data = vec![0; ZeroCopySliceMut::::required_size_for_capacity(length)]; let zero_copy_slice = ZeroCopySliceMut::::new(u8::try_from(length).unwrap(), &mut data).unwrap(); - zero_copy_slice[length]; + zero_copy_slice[length as usize]; } /// Test that metadata size is aligned to T. @@ -280,19 +270,19 @@ fn test_required_size() { 64 + 1 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u16), 64 + 2 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u32), 64 + 4 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u64), 64 + 8 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as usize), 64 + 8 ); @@ -301,19 +291,19 @@ fn test_required_size() { 128 + 2 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u16), 128 + 2 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u32), 128 + 4 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u64), 128 + 8 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as usize), 128 + 8 ); @@ -322,19 +312,19 @@ fn test_required_size() { 256 + 4 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u16), 256 + 4 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u32), 256 + 4 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u64), 256 + 8 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as usize), 256 + 8 ); @@ -343,19 +333,19 @@ fn test_required_size() { 512 + 8 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u16), 512 + 8 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u32), 512 + 8 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as u64), 512 + 8 ); assert_eq!( - ZeroCopySliceMut::::required_size_for_capacity(length), + ZeroCopySliceMut::::required_size_for_capacity(length as usize), 512 + 8 ); } diff --git a/program-libs/zero-copy/tests/vec_tests.rs b/program-libs/zero-copy/tests/vec_tests.rs index c20f6a1cd..aa4ad3830 100644 --- a/program-libs/zero-copy/tests/vec_tests.rs +++ b/program-libs/zero-copy/tests/vec_tests.rs @@ -1,10 +1,11 @@ -use std::{fmt::Debug, ops::Add, u8}; +use std::fmt::Debug; use light_zero_copy::{ add_padding, errors::ZeroCopyError, vec::{ZeroCopyVec, ZeroCopyVecUsize}, }; +use num_traits::{FromPrimitive, PrimInt, ToPrimitive}; use rand::{ distributions::{Distribution, Standard}, thread_rng, Rng, @@ -96,16 +97,7 @@ fn test_zero_copy_u8_struct_vec() { fn test_zero_copy_vec_new(capacity: CAPACITY) where - CAPACITY: Debug - + Copy - + Clone - + Add - + TryInto - + TryFrom - + PartialOrd - + num_traits::ToBytes, - >::Error: std::fmt::Debug, - >::Error: std::fmt::Debug, + CAPACITY: FromPrimitive + ToPrimitive + PrimInt + num_traits::ToBytes + Debug, T: Copy + Clone + PartialEq + Debug, Standard: Distribution, { @@ -113,14 +105,14 @@ where let mut data = vec![ 0; - ZeroCopyVec::::required_size_for_capacity(capacity.try_into().unwrap()) + ZeroCopyVec::::required_size_for_capacity(capacity.to_usize().unwrap()) ]; println!("data len: {}", data.len()); println!("capacity: {:?}", capacity); // new { let zero_copy_vec = ZeroCopyVec::::new(capacity, &mut data).unwrap(); - assert_eq!(zero_copy_vec.capacity(), capacity.try_into().unwrap()); + assert_eq!(zero_copy_vec.capacity(), capacity.to_usize().unwrap()); assert_eq!(zero_copy_vec.len(), 0); assert!(zero_copy_vec.is_empty()); } @@ -133,7 +125,7 @@ where let data = data.clone(); let mut metadata_size = size_of::(); let length = data[0..metadata_size].to_vec(); - let ref_length: CAPACITY = (0).try_into().unwrap(); + let ref_length: CAPACITY = CAPACITY::from_usize(0).unwrap(); assert_eq!(length, ref_length.to_ne_bytes().as_ref().to_vec()); let padding_start = metadata_size.clone(); @@ -144,7 +136,7 @@ where assert_eq!(data, vec![0; padding_end - padding_start]); } } - let capacity_usize: usize = capacity.try_into().unwrap(); + let capacity_usize: usize = capacity.to_usize().unwrap(); let mut reference_vec = vec![]; // fill vector completely and assert: { @@ -176,7 +168,7 @@ where let data = data.clone(); let mut metadata_size = size_of::(); let length = data[0..metadata_size].to_vec(); - let ref_length: CAPACITY = (i + 1).try_into().unwrap(); + let ref_length: CAPACITY = CAPACITY::from_usize(i + 1).unwrap(); assert_eq!(length, ref_length.to_ne_bytes().as_ref().to_vec()); let padding_start = metadata_size.clone(); @@ -255,7 +247,7 @@ where let data = data.clone(); let mut metadata_size = size_of::(); let length = data[0..metadata_size].to_vec(); - let ref_length: CAPACITY = (0).try_into().unwrap(); + let ref_length: CAPACITY = CAPACITY::zero(); //;(0).to_usize().unwrap(); assert_eq!(length, ref_length.to_ne_bytes().as_ref().to_vec()); let padding_start = metadata_size.clone(); @@ -275,20 +267,12 @@ fn assert_full_vec( reference_vec: &mut Vec, vec: &mut ZeroCopyVec, ) where - CAPACITY: Debug - + Copy - + Clone - + Add - + TryInto - + TryFrom - + PartialOrd, + CAPACITY: FromPrimitive + ToPrimitive + PrimInt, T: Copy + Clone + PartialEq + Debug, Standard: Distribution, - >::Error: std::fmt::Debug, - >::Error: std::fmt::Debug, { // 1. vector capacity is correct - assert_eq!(vec.capacity(), capacity.try_into().unwrap()); + assert_eq!(vec.capacity(), capacity.to_usize().unwrap()); // 2. vector length is correct assert_eq!(vec.len(), capacity_usize); // 3. vector is not empty @@ -320,19 +304,11 @@ fn assert_empty_vec( mut reference_vec: Vec, mut vec: ZeroCopyVec, ) where - CAPACITY: Debug - + Copy - + Clone - + Add - + TryInto - + TryFrom - + PartialOrd, + CAPACITY: FromPrimitive + ToPrimitive + PrimInt, T: Copy + Clone + PartialEq + Debug, - >::Error: std::fmt::Debug, - >::Error: std::fmt::Debug, { // 1. vector capacity is correct - assert_eq!(vec.capacity(), capacity.try_into().unwrap()); + assert_eq!(vec.capacity(), capacity.to_usize().unwrap()); // 2. vector length is correct assert_eq!(vec.len(), 0); // 3. vector is empty