Skip to content

Commit

Permalink
chore: switch ne_bytes to le_bytes (#1225)
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block authored Sep 16, 2024
1 parent dd89e91 commit e499c4b
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 74 deletions.
40 changes: 20 additions & 20 deletions merkle-tree/bounded-vec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ impl BoundedVecMetadata {
Self { capacity, length }
}

pub fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
pub fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
Self {
capacity: usize::from_ne_bytes(bytes[span_of!(Self, capacity)].try_into().unwrap()),
length: usize::from_ne_bytes(bytes[span_of!(Self, length)].try_into().unwrap()),
capacity: usize::from_le_bytes(bytes[span_of!(Self, capacity)].try_into().unwrap()),
length: usize::from_le_bytes(bytes[span_of!(Self, length)].try_into().unwrap()),
}
}

pub fn to_ne_bytes(&self) -> [u8; mem::size_of::<Self>()] {
pub fn to_le_bytes(&self) -> [u8; mem::size_of::<Self>()] {
let mut bytes = [0u8; mem::size_of::<Self>()];
bytes[span_of!(Self, capacity)].copy_from_slice(&self.capacity.to_ne_bytes());
bytes[span_of!(Self, length)].copy_from_slice(&self.length.to_ne_bytes());
bytes[span_of!(Self, capacity)].copy_from_slice(&self.capacity.to_le_bytes());
bytes[span_of!(Self, length)].copy_from_slice(&self.length.to_le_bytes());

bytes
}
Expand Down Expand Up @@ -500,23 +500,23 @@ impl CyclicBoundedVecMetadata {
}
}

pub fn from_ne_bytes(bytes: [u8; mem::size_of::<CyclicBoundedVecMetadata>()]) -> Self {
pub fn from_le_bytes(bytes: [u8; mem::size_of::<CyclicBoundedVecMetadata>()]) -> Self {
Self {
capacity: usize::from_ne_bytes(bytes[span_of!(Self, capacity)].try_into().unwrap()),
length: usize::from_ne_bytes(bytes[span_of!(Self, length)].try_into().unwrap()),
first_index: usize::from_ne_bytes(
capacity: usize::from_le_bytes(bytes[span_of!(Self, capacity)].try_into().unwrap()),
length: usize::from_le_bytes(bytes[span_of!(Self, length)].try_into().unwrap()),
first_index: usize::from_le_bytes(
bytes[span_of!(Self, first_index)].try_into().unwrap(),
),
last_index: usize::from_ne_bytes(bytes[span_of!(Self, last_index)].try_into().unwrap()),
last_index: usize::from_le_bytes(bytes[span_of!(Self, last_index)].try_into().unwrap()),
}
}

pub fn to_ne_bytes(&self) -> [u8; mem::size_of::<Self>()] {
pub fn to_le_bytes(&self) -> [u8; mem::size_of::<Self>()] {
let mut bytes = [0u8; mem::size_of::<Self>()];
bytes[span_of!(Self, capacity)].copy_from_slice(&self.capacity.to_ne_bytes());
bytes[span_of!(Self, length)].copy_from_slice(&self.length.to_ne_bytes());
bytes[span_of!(Self, first_index)].copy_from_slice(&self.first_index.to_ne_bytes());
bytes[span_of!(Self, last_index)].copy_from_slice(&self.last_index.to_ne_bytes());
bytes[span_of!(Self, capacity)].copy_from_slice(&self.capacity.to_le_bytes());
bytes[span_of!(Self, length)].copy_from_slice(&self.length.to_le_bytes());
bytes[span_of!(Self, first_index)].copy_from_slice(&self.first_index.to_le_bytes());
bytes[span_of!(Self, last_index)].copy_from_slice(&self.last_index.to_le_bytes());

bytes
}
Expand Down Expand Up @@ -960,8 +960,8 @@ mod test {
assert_eq!(metadata.capacity(), capacity);
assert_eq!(metadata.length(), 0);

let bytes = metadata.to_ne_bytes();
let metadata_2 = BoundedVecMetadata::from_ne_bytes(bytes);
let bytes = metadata.to_le_bytes();
let metadata_2 = BoundedVecMetadata::from_le_bytes(bytes);

assert_eq!(metadata, metadata_2);
}
Expand Down Expand Up @@ -1229,8 +1229,8 @@ mod test {
assert_eq!(metadata.capacity(), capacity);
assert_eq!(metadata.length(), 0);

let bytes = metadata.to_ne_bytes();
let metadata_2 = CyclicBoundedVecMetadata::from_ne_bytes(bytes);
let bytes = metadata.to_le_bytes();
let metadata_2 = CyclicBoundedVecMetadata::from_le_bytes(bytes);

assert_eq!(metadata, metadata_2);
}
Expand Down
4 changes: 2 additions & 2 deletions merkle-tree/concurrent/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ where
));
}

let height = usize::from_ne_bytes(
let height = usize::from_le_bytes(
bytes[span_of!(ConcurrentMerkleTree<H, HEIGHT>, height)]
.try_into()
.unwrap(),
);
let canopy_depth = usize::from_ne_bytes(
let canopy_depth = usize::from_le_bytes(
bytes[span_of!(ConcurrentMerkleTree<H, HEIGHT>, canopy_depth)]
.try_into()
.unwrap(),
Expand Down
20 changes: 10 additions & 10 deletions merkle-tree/concurrent/src/zero_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ where
));
}

let height = usize::from_ne_bytes(
let height = usize::from_le_bytes(
bytes[span_of!(ConcurrentMerkleTree<H, HEIGHT>, height)]
.try_into()
.unwrap(),
);
let canopy_depth = usize::from_ne_bytes(
let canopy_depth = usize::from_le_bytes(
bytes[span_of!(ConcurrentMerkleTree<H, HEIGHT>, canopy_depth)]
.try_into()
.unwrap(),
Expand Down Expand Up @@ -198,30 +198,30 @@ where
}

bytes[span_of!(ConcurrentMerkleTree<H, HEIGHT>, height)]
.copy_from_slice(&height.to_ne_bytes());
.copy_from_slice(&height.to_le_bytes());
bytes[span_of!(ConcurrentMerkleTree<H, HEIGHT>, canopy_depth)]
.copy_from_slice(&canopy_depth.to_ne_bytes());
.copy_from_slice(&canopy_depth.to_le_bytes());

let mut offset = offset_of!(ConcurrentMerkleTree<H, HEIGHT>, next_index);
// next_index
write_at::<usize>(bytes, &0_usize.to_ne_bytes(), &mut offset);
write_at::<usize>(bytes, &0_usize.to_le_bytes(), &mut offset);
// sequence_number
write_at::<usize>(bytes, &0_usize.to_ne_bytes(), &mut offset);
write_at::<usize>(bytes, &0_usize.to_le_bytes(), &mut offset);
// rightmost_leaf
write_at::<[u8; 32]>(bytes, &H::zero_bytes()[0], &mut offset);
// filled_subtrees (metadata)
let filled_subtrees_metadata = BoundedVecMetadata::new(height);
write_at::<BoundedVecMetadata>(bytes, &filled_subtrees_metadata.to_ne_bytes(), &mut offset);
write_at::<BoundedVecMetadata>(bytes, &filled_subtrees_metadata.to_le_bytes(), &mut offset);
// changelog (metadata)
let changelog_metadata = CyclicBoundedVecMetadata::new(changelog_capacity);
write_at::<CyclicBoundedVecMetadata>(bytes, &changelog_metadata.to_ne_bytes(), &mut offset);
write_at::<CyclicBoundedVecMetadata>(bytes, &changelog_metadata.to_le_bytes(), &mut offset);
// roots (metadata)
let roots_metadata = CyclicBoundedVecMetadata::new(roots_capacity);
write_at::<CyclicBoundedVecMetadata>(bytes, &roots_metadata.to_ne_bytes(), &mut offset);
write_at::<CyclicBoundedVecMetadata>(bytes, &roots_metadata.to_le_bytes(), &mut offset);
// canopy (metadata)
let canopy_size = ConcurrentMerkleTree::<H, HEIGHT>::canopy_size(canopy_depth);
let canopy_metadata = BoundedVecMetadata::new(canopy_size);
write_at::<BoundedVecMetadata>(bytes, &canopy_metadata.to_ne_bytes(), &mut offset);
write_at::<BoundedVecMetadata>(bytes, &canopy_metadata.to_le_bytes(), &mut offset);

Ok(offset)
}
Expand Down
4 changes: 2 additions & 2 deletions merkle-tree/hash-set/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ impl HashSet {
));
}

let capacity = usize::from_ne_bytes(bytes[0..8].try_into().unwrap());
let sequence_threshold = usize::from_ne_bytes(bytes[8..16].try_into().unwrap());
let capacity = usize::from_le_bytes(bytes[0..8].try_into().unwrap());
let sequence_threshold = usize::from_le_bytes(bytes[8..16].try_into().unwrap());
let expected_size = Self::size_in_account(capacity);
if bytes.len() != expected_size {
return Err(HashSetError::BufferSize(expected_size, bytes.len()));
Expand Down
10 changes: 5 additions & 5 deletions merkle-tree/hash-set/src/zero_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl<'a> HashSetZeroCopy<'a> {
));
}

let capacity_values = usize::from_ne_bytes(bytes[0..8].try_into().unwrap());
let sequence_threshold = usize::from_ne_bytes(bytes[8..16].try_into().unwrap());
let capacity_values = usize::from_le_bytes(bytes[0..8].try_into().unwrap());
let sequence_threshold = usize::from_le_bytes(bytes[8..16].try_into().unwrap());

let offset = HashSet::non_dyn_fields_size() + mem::size_of::<usize>();

Expand Down Expand Up @@ -105,9 +105,9 @@ impl<'a> HashSetZeroCopy<'a> {
));
}

bytes[0..8].copy_from_slice(&capacity_values.to_ne_bytes());
bytes[8..16].copy_from_slice(&sequence_threshold.to_ne_bytes());
bytes[16..24].copy_from_slice(&0_usize.to_ne_bytes());
bytes[0..8].copy_from_slice(&capacity_values.to_le_bytes());
bytes[8..16].copy_from_slice(&sequence_threshold.to_le_bytes());
bytes[16..24].copy_from_slice(&0_usize.to_le_bytes());

let hash_set = Self::from_bytes_zero_copy_mut(bytes)?;

Expand Down
44 changes: 22 additions & 22 deletions merkle-tree/hasher/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ macro_rules! impl_as_byte_vec_for_integer_type {
($int_ty:ty) => {
impl AsByteVec for $int_ty {
fn as_byte_vec(&self) -> Vec<Vec<u8>> {
vec![self.to_ne_bytes().to_vec()]
vec![self.to_le_bytes().to_vec()]
}
}
};
Expand All @@ -30,7 +30,7 @@ macro_rules! impl_as_byte_vec_for_primitive_type {
// chance of undefined behavior.
// - Unfortunately, there is no way to achieve the similar
// result with fully safe code. If we tried to do anything
// like `&self.to_ne_bytes()` or `self.to_ne_bytes().as_slice()`,
// like `&self.to_le_bytes()` or `self.to_le_bytes().as_slice()`,
// compiler would complain with "cannot return reference to
// temporary value".
let self_byte_slice = unsafe { slice::from_raw_parts(self_ptr.cast::<u8>(), len) };
Expand Down Expand Up @@ -100,104 +100,104 @@ mod test {
let i8_min: &dyn AsByteVec = &i8::MIN;
let i8_min_bytes = i8_min.as_byte_vec();
assert_eq!(i8_min_bytes, &[&[128]]);
assert_eq!(i8_min_bytes, &[i8::MIN.to_ne_bytes()]);
assert_eq!(i8_min_bytes, &[i8::MIN.to_le_bytes()]);
let i8_max: &dyn AsByteVec = &i8::MAX;
let i8_max_bytes = i8_max.as_byte_vec();
assert_eq!(i8_max_bytes, &[&[127]]);
assert_eq!(i8_max_bytes, &[i8::MAX.to_ne_bytes()]);
assert_eq!(i8_max_bytes, &[i8::MAX.to_le_bytes()]);

let u8_min: &dyn AsByteVec = &u8::MIN;
let u8_min_bytes = u8_min.as_byte_vec();
assert_eq!(u8_min_bytes, &[&[0]]);
assert_eq!(u8_min_bytes, &[u8::MIN.to_ne_bytes()]);
assert_eq!(u8_min_bytes, &[u8::MIN.to_le_bytes()]);
let u8_max: &dyn AsByteVec = &u8::MAX;
let u8_max_bytes = u8_max.as_byte_vec();
assert_eq!(u8_max_bytes, &[&[255]]);
assert_eq!(u8_max_bytes, &[u8::MAX.to_ne_bytes()]);
assert_eq!(u8_max_bytes, &[u8::MAX.to_le_bytes()]);

let i16_min: &dyn AsByteVec = &i16::MIN;
let i16_min_bytes = i16_min.as_byte_vec();
assert_eq!(i16_min_bytes, &[&[0, 128]]);
assert_eq!(i16_min_bytes, &[&i16::MIN.to_ne_bytes()]);
assert_eq!(i16_min_bytes, &[&i16::MIN.to_le_bytes()]);
let i16_max: &dyn AsByteVec = &i16::MAX;
let i16_max_bytes = i16_max.as_byte_vec();
assert_eq!(i16_max_bytes, &[&[255, 127]]);
assert_eq!(i16_max_bytes, &[i16::MAX.to_ne_bytes()]);
assert_eq!(i16_max_bytes, &[i16::MAX.to_le_bytes()]);

let u16_min: &dyn AsByteVec = &u16::MIN;
let u16_min_bytes = u16_min.as_byte_vec();
assert_eq!(u16_min_bytes, &[&[0, 0]]);
assert_eq!(u16_min_bytes, &[u16::MIN.to_ne_bytes()]);
assert_eq!(u16_min_bytes, &[u16::MIN.to_le_bytes()]);
let u16_max: &dyn AsByteVec = &u16::MAX;
let u16_max_bytes = u16_max.as_byte_vec();
assert_eq!(u16_max_bytes, &[&[255, 255]]);
assert_eq!(u16_max_bytes, &[u16::MAX.to_ne_bytes()]);
assert_eq!(u16_max_bytes, &[u16::MAX.to_le_bytes()]);

let i32_min: &dyn AsByteVec = &i32::MIN;
let i32_min_bytes = i32_min.as_byte_vec();
assert_eq!(i32_min_bytes, &[&[0, 0, 0, 128]]);
assert_eq!(i32_min_bytes, &[i32::MIN.to_ne_bytes()]);
assert_eq!(i32_min_bytes, &[i32::MIN.to_le_bytes()]);
let i32_max: &dyn AsByteVec = &i32::MAX;
let i32_max_bytes = i32_max.as_byte_vec();
assert_eq!(i32_max_bytes, &[&[255, 255, 255, 127]]);
assert_eq!(i32_max_bytes, &[i32::MAX.to_ne_bytes()]);
assert_eq!(i32_max_bytes, &[i32::MAX.to_le_bytes()]);

let u32_min: &dyn AsByteVec = &u32::MIN;
let u32_min_bytes = u32_min.as_byte_vec();
assert_eq!(u32_min_bytes, &[&[0, 0, 0, 0]]);
assert_eq!(u32_min_bytes, &[u32::MIN.to_ne_bytes()]);
assert_eq!(u32_min_bytes, &[u32::MIN.to_le_bytes()]);
let u32_max: &dyn AsByteVec = &u32::MAX;
let u32_max_bytes = u32_max.as_byte_vec();
assert_eq!(u32_max_bytes, &[&[255, 255, 255, 255]]);
assert_eq!(u32_max_bytes, &[u32::MAX.to_ne_bytes()]);
assert_eq!(u32_max_bytes, &[u32::MAX.to_le_bytes()]);

let i64_min: &dyn AsByteVec = &i64::MIN;
let i64_min_bytes = i64_min.as_byte_vec();
assert_eq!(i64_min_bytes, &[&[0, 0, 0, 0, 0, 0, 0, 128]]);
assert_eq!(i64_min_bytes, &[i64::MIN.to_ne_bytes()]);
assert_eq!(i64_min_bytes, &[i64::MIN.to_le_bytes()]);
let i64_max: &dyn AsByteVec = &i64::MAX;
let i64_max_bytes = i64_max.as_byte_vec();
assert_eq!(i64_max_bytes, &[&[255, 255, 255, 255, 255, 255, 255, 127]]);
assert_eq!(i64_max_bytes, &[i64::MAX.to_ne_bytes()]);
assert_eq!(i64_max_bytes, &[i64::MAX.to_le_bytes()]);

let u64_min: &dyn AsByteVec = &u64::MIN;
let u64_min_bytes = u64_min.as_byte_vec();
assert_eq!(u64_min_bytes, &[[0, 0, 0, 0, 0, 0, 0, 0]]);
assert_eq!(i64_min_bytes, &[i64::MIN.to_ne_bytes()]);
assert_eq!(i64_min_bytes, &[i64::MIN.to_le_bytes()]);
let u64_max: &dyn AsByteVec = &u64::MAX;
let u64_max_bytes = u64_max.as_byte_vec();
assert_eq!(u64_max_bytes, &[&[255, 255, 255, 255, 255, 255, 255, 255]]);
assert_eq!(u64_max_bytes, &[u64::MAX.to_ne_bytes()]);
assert_eq!(u64_max_bytes, &[u64::MAX.to_le_bytes()]);

let i128_min: &dyn AsByteVec = &i128::MIN;
let i128_min_bytes = i128_min.as_byte_vec();
assert_eq!(
i128_min_bytes,
&[&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128]]
);
assert_eq!(i128_min_bytes, &[i128::MIN.to_ne_bytes()]);
assert_eq!(i128_min_bytes, &[i128::MIN.to_le_bytes()]);
let i128_max: &dyn AsByteVec = &i128::MAX;
let i128_max_bytes = i128_max.as_byte_vec();
assert_eq!(
i128_max_bytes,
&[&[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127]]
);
assert_eq!(i128_max_bytes, &[i128::MAX.to_ne_bytes()]);
assert_eq!(i128_max_bytes, &[i128::MAX.to_le_bytes()]);

let u128_min: &dyn AsByteVec = &u128::MIN;
let u128_min_bytes = u128_min.as_byte_vec();
assert_eq!(
u128_min_bytes,
&[&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
);
assert_eq!(u128_min_bytes, &[u128::MIN.to_ne_bytes()]);
assert_eq!(u128_min_bytes, &[u128::MIN.to_le_bytes()]);
let u128_max: &dyn AsByteVec = &u128::MAX;
let u128_max_bytes = u128_max.as_byte_vec();
assert_eq!(
u128_max_bytes,
&[&[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]]
);
assert_eq!(u128_max_bytes, &[u128::MAX.to_ne_bytes()]);
assert_eq!(u128_max_bytes, &[u128::MAX.to_le_bytes()]);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion merkle-tree/indexed/src/zero_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ where
let indexed_changelog_metadata = CyclicBoundedVecMetadata::new(indexed_changelog_capacity);
write_at::<CyclicBoundedVecMetadata>(
bytes,
&indexed_changelog_metadata.to_ne_bytes(),
&indexed_changelog_metadata.to_le_bytes(),
&mut offset,
);

Expand Down
4 changes: 2 additions & 2 deletions utils/src/offset/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ mod test {
vec.push(37);
vec.push(49);

let metadata_bytes = vec.metadata().to_ne_bytes();
let metadata = CyclicBoundedVecMetadata::from_ne_bytes(metadata_bytes);
let metadata_bytes = vec.metadata().to_le_bytes();
let metadata = CyclicBoundedVecMetadata::from_le_bytes(metadata_bytes);
let bytes = unsafe {
slice::from_raw_parts(
vec.as_mut_ptr() as *mut u8,
Expand Down
Loading

0 comments on commit e499c4b

Please sign in to comment.