Skip to content

Commit

Permalink
chore: clean up trait bounds use FromPrimitive + ToPrimitive + PrimInt
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block committed Jan 4, 2025
1 parent 95e9bf0 commit 0ed92f0
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 257 deletions.
1 change: 1 addition & 0 deletions program-libs/zero-copy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
112 changes: 29 additions & 83 deletions program-libs/zero-copy/src/cyclic_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -18,26 +20,17 @@ pub type ZeroCopyCyclicVecU8<T> = ZeroCopyCyclicVec<u8, T>;

pub struct ZeroCopyCyclicVec<LEN, T>
where
LEN: TryFrom<usize> + Clone + Copy + TryInto<usize> + Add<LEN, Output = LEN>,
T: Copy + Clone,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy,
{
current_index: WrappedPointerMut<LEN>,
vec: ZeroCopyVec<LEN, T>,
}

impl<LEN, T> ZeroCopyCyclicVec<LEN, T>
where
LEN: TryFrom<usize>
+ TryInto<usize>
+ Copy
+ Clone
+ Add<LEN, Output = LEN>
+ Rem<LEN, Output = LEN>,
T: Copy + Clone,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy,
{
pub fn new(capacity: LEN, vec: &mut [u8]) -> Result<Self, ZeroCopyError> {
Self::new_at(capacity, vec, &mut 0)
Expand All @@ -48,8 +41,7 @@ where
vec: &mut [u8],
offset: &mut usize,
) -> Result<Self, ZeroCopyError> {
let current_index =
WrappedPointerMut::<LEN>::new_at(LEN::try_from(0).unwrap(), vec, offset)?;
let current_index = WrappedPointerMut::<LEN>::new_at(LEN::zero(), vec, offset)?;
add_padding::<LEN, T>(offset);
let vec = ZeroCopyVec::<LEN, T>::new_at(capacity, vec, offset)?;
Ok(Self { current_index, vec })
Expand All @@ -72,15 +64,8 @@ where

impl<LEN, T> ZeroCopyCyclicVec<LEN, T>
where
LEN: TryFrom<usize>
+ TryInto<usize>
+ Copy
+ Clone
+ Add<LEN, Output = LEN>
+ Rem<LEN, Output = LEN>,
T: Copy + Clone,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy,
{
pub fn from_bytes(account_data: &mut [u8]) -> Result<Self, ZeroCopyError> {
Self::from_bytes_at(account_data, &mut 0)
Expand Down Expand Up @@ -112,15 +97,8 @@ where

impl<LEN, T> ZeroCopyCyclicVec<LEN, T>
where
LEN: TryFrom<usize>
+ TryInto<usize>
+ Copy
+ Clone
+ Add<LEN, Output = LEN>
+ Rem<LEN, Output = LEN>,
T: Copy + Clone,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy,
{
#[inline]
pub fn push(&mut self, value: T) {
Expand All @@ -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();
}

Expand All @@ -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.
Expand Down Expand Up @@ -217,11 +195,11 @@ where
}

pub fn data_size(length: LEN) -> usize {
ZeroCopyVec::<LEN, T>::required_size_for_capacity(length.try_into().unwrap())
ZeroCopyVec::<LEN, T>::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]
Expand Down Expand Up @@ -271,10 +249,8 @@ where

pub struct ZeroCopyCyclicVecIterator<'a, LEN, T>
where
LEN: TryFrom<usize> + Clone + Copy + TryInto<usize> + Add<LEN, Output = LEN>,
T: Copy + Clone,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy,
{
vec: &'a ZeroCopyCyclicVec<LEN, T>,
current: usize,
Expand All @@ -284,15 +260,8 @@ where

impl<'a, LEN, T> Iterator for ZeroCopyCyclicVecIterator<'a, LEN, T>
where
LEN: TryFrom<usize>
+ TryInto<usize>
+ Copy
+ Clone
+ Rem<LEN, Output = LEN>
+ Add<LEN, Output = LEN>,
T: Copy + Clone,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy,
{
type Item = &'a T;

Expand All @@ -314,15 +283,8 @@ where

impl<LEN, T> IndexMut<usize> for ZeroCopyCyclicVec<LEN, T>
where
LEN: TryFrom<usize>
+ TryInto<usize>
+ Copy
+ Clone
+ Add<LEN, Output = LEN>
+ Rem<LEN, Output = LEN>,
T: Copy + Clone,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy,
{
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
Expand All @@ -333,15 +295,8 @@ where

impl<LEN, T> Index<usize> for ZeroCopyCyclicVec<LEN, T>
where
LEN: TryFrom<usize>
+ TryInto<usize>
+ Copy
+ Clone
+ Add<LEN, Output = LEN>
+ Rem<LEN, Output = LEN>,
T: Copy + Clone,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy,
{
type Output = T;

Expand All @@ -354,10 +309,8 @@ where

impl<LEN, T> PartialEq for ZeroCopyCyclicVec<LEN, T>
where
LEN: TryFrom<usize> + TryInto<usize> + Copy + Clone + Add<LEN, Output = LEN> + PartialEq,
T: Copy + Clone + PartialEq,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy + PartialEq,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
Expand All @@ -367,15 +320,8 @@ where

impl<LEN, T> fmt::Debug for ZeroCopyCyclicVec<LEN, T>
where
LEN: TryFrom<usize>
+ TryInto<usize>
+ Copy
+ Clone
+ Add<LEN, Output = LEN>
+ Rem<LEN, Output = LEN>,
T: Copy + Clone + Debug,
<LEN as TryFrom<usize>>::Error: fmt::Debug,
<LEN as TryInto<usize>>::Error: fmt::Debug,
LEN: FromPrimitive + ToPrimitive + PrimInt,
T: Copy + Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
Loading

0 comments on commit 0ed92f0

Please sign in to comment.