From 3d49075c358e4017dccda5642a87684357e1e6ae Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Fri, 21 Jun 2024 09:59:39 +0200 Subject: [PATCH] chore: Address review comments * Use `MIN` and `MAX` values of primitive types. * Read vectors with `[u8; 32]` as a type. --- utils/src/offset/copy.rs | 151 +++++++++++++++++++++++++++++++-------- 1 file changed, 120 insertions(+), 31 deletions(-) diff --git a/utils/src/offset/copy.rs b/utils/src/offset/copy.rs index 51d9dbbab2..36454b037c 100644 --- a/utils/src/offset/copy.rs +++ b/utils/src/offset/copy.rs @@ -18,7 +18,6 @@ where let size = mem::size_of::(); let ptr = bytes[*offset..*offset + size].as_ptr() as *const T; *offset += size; - // (*ptr).clone() ptr::read(ptr) } @@ -29,6 +28,10 @@ where /// This is higly unsafe. This function doesn't ensure alignment and /// correctness of provided buffer. The responsibility of such checks is on /// the caller. +/// +/// The `T` type needs to be either a primitive or struct consisting of +/// primitives. It cannot contain any nested heap-backed stucture (like vectors, +/// slices etc.). pub unsafe fn read_bounded_vec_at( bytes: &[u8], offset: &mut usize, @@ -119,74 +122,65 @@ mod test { let s = buf.as_mut_ptr() as *mut TestStruct; unsafe { - (*s).a = -9223372036854771632; - (*s).b = 9223372036854771632; - (*s).c = -9223372036854771632; - (*s).d = 9223372036854771632; - (*s).e = -2147483623; - (*s).f = 2147483623; - (*s).g = -32721; - (*s).h = 32721; - (*s).i = -127; - (*s).j = 127; + (*s).a = isize::MIN; + (*s).b = usize::MAX; + (*s).c = i64::MIN; + (*s).d = u64::MAX; + (*s).e = i32::MIN; + (*s).f = u32::MAX; + (*s).g = i16::MIN; + (*s).h = u16::MAX; + (*s).i = i8::MIN; + (*s).j = u8::MAX; let mut offset = offset_of!(TestStruct, a); assert_eq!(offset, 0); - assert_eq!( - read_value_at::(&buf, &mut offset), - -9223372036854771632 - ); + assert_eq!(read_value_at::(&buf, &mut offset), isize::MIN); assert_eq!(offset, 8); let mut offset = offset_of!(TestStruct, b); assert_eq!(offset, 8); - assert_eq!( - read_value_at::(&buf, &mut offset), - 9223372036854771632 - ); + assert_eq!(read_value_at::(&buf, &mut offset), usize::MAX); assert_eq!(offset, 16); let mut offset = offset_of!(TestStruct, c); assert_eq!(offset, 16); - assert_eq!( - read_value_at::(&buf, &mut offset), - -9223372036854771632 - ); + assert_eq!(read_value_at::(&buf, &mut offset), i64::MIN); assert_eq!(offset, 24); let mut offset = offset_of!(TestStruct, d); assert_eq!(offset, 24); - assert_eq!(read_value_at::(&buf, &mut offset), 9223372036854771632); + assert_eq!(read_value_at::(&buf, &mut offset), u64::MAX); assert_eq!(offset, 32); let mut offset = offset_of!(TestStruct, e); assert_eq!(offset, 32); - assert_eq!(read_value_at::(&buf, &mut offset), -2147483623); + assert_eq!(read_value_at::(&buf, &mut offset), i32::MIN); assert_eq!(offset, 36); let mut offset = offset_of!(TestStruct, f); assert_eq!(offset, 36); - assert_eq!(read_value_at::(&buf, &mut offset), 2147483623); + assert_eq!(read_value_at::(&buf, &mut offset), u32::MAX); assert_eq!(offset, 40); let mut offset = offset_of!(TestStruct, g); assert_eq!(offset, 40); - assert_eq!(read_value_at::(&buf, &mut offset), -32721); + assert_eq!(read_value_at::(&buf, &mut offset), i16::MIN); assert_eq!(offset, 42); let mut offset = offset_of!(TestStruct, h); assert_eq!(offset, 44); - assert_eq!(read_value_at::(&buf, &mut offset), 32721); + assert_eq!(read_value_at::(&buf, &mut offset), u16::MAX); assert_eq!(offset, 46); let mut offset = offset_of!(TestStruct, i); assert_eq!(offset, 48); - assert_eq!(read_value_at::(&buf, &mut offset), -127); + assert_eq!(read_value_at::(&buf, &mut offset), i8::MIN); assert_eq!(offset, 49); let mut offset = offset_of!(TestStruct, j); assert_eq!(offset, 52); - assert_eq!(read_value_at::(&buf, &mut offset), 127); + assert_eq!(read_value_at::(&buf, &mut offset), u8::MAX); assert_eq!(offset, 53); } } @@ -198,6 +192,7 @@ mod test { struct TestStruct { a: [i64; 32], b: [u64; 32], + c: [[u8; 32]; 32], } let mut buf = vec![0_u8; mem::size_of::()]; @@ -210,6 +205,9 @@ mod test { for (i, element) in (*s).b.iter_mut().enumerate() { *element = i as u64; } + for (i, element) in (*s).c.iter_mut().enumerate() { + *element = [i as u8; 32]; + } let metadata = BoundedVecMetadata::new_with_length(32, 32); let mut offset = offset_of!(TestStruct, a); @@ -218,6 +216,25 @@ mod test { for (i, element) in vec.iter().enumerate() { assert_eq!(i as i64, -(*element as i64)); } + assert_eq!(offset, 256); + + let metadata = BoundedVecMetadata::new_with_length(32, 32); + let mut offset = offset_of!(TestStruct, b); + assert_eq!(offset, 256); + let vec: BoundedVec = read_bounded_vec_at(&buf, &mut offset, &metadata); + for (i, element) in vec.iter().enumerate() { + assert_eq!(i as u64, *element as u64); + } + assert_eq!(offset, 512); + + let metadata = BoundedVecMetadata::new_with_length(32, 32); + let mut offset = offset_of!(TestStruct, c); + assert_eq!(offset, 512); + let vec: BoundedVec<[u8; 32]> = read_bounded_vec_at(&buf, &mut offset, &metadata); + for (i, element) in vec.iter().enumerate() { + assert_eq!(&[i as u8; 32], element); + } + assert_eq!(offset, 1536); } } @@ -228,6 +245,7 @@ mod test { struct TestStruct { a: [i64; 32], b: [u64; 32], + c: [[u8; 32]; 32], } let mut buf = vec![0_u8; mem::size_of::()]; @@ -240,8 +258,12 @@ mod test { for (i, element) in (*s).b.iter_mut().enumerate() { *element = i as u64; } + for (i, element) in (*s).c.iter_mut().enumerate() { + *element = [i as u8; 32]; + } + + // Start the cyclic vecs from the middle. - // Start the cyclic vec from the middle. let metadata = CyclicBoundedVecMetadata::new_with_indices(32, 32, 14, 13); let mut offset = offset_of!(TestStruct, a); assert_eq!(offset, 0); @@ -259,6 +281,73 @@ mod test { &-10, &-11, &-12, &-13 ] ); + assert_eq!(offset, 256); + + let metadata = CyclicBoundedVecMetadata::new_with_indices(32, 32, 14, 13); + let mut offset = offset_of!(TestStruct, b); + assert_eq!(offset, 256); + let vec: CyclicBoundedVec = + read_cyclic_bounded_vec_at(&buf, &mut offset, &metadata); + assert_eq!(vec.capacity(), 32); + assert_eq!(vec.len(), 32); + assert_eq!(vec.first_index(), 14); + assert_eq!(vec.last_index(), 13); + assert_eq!( + vec.iter().collect::>().as_slice(), + &[ + &14, &15, &16, &17, &18, &19, &20, &21, &22, &23, &24, &25, &26, &27, &28, &29, + &30, &31, &0, &1, &2, &3, &4, &5, &6, &7, &8, &9, &10, &11, &12, &13 + ] + ); + assert_eq!(offset, 512); + + let metadata = CyclicBoundedVecMetadata::new_with_indices(32, 32, 14, 13); + let mut offset = offset_of!(TestStruct, c); + assert_eq!(offset, 512); + let vec: CyclicBoundedVec<[u8; 32]> = + read_cyclic_bounded_vec_at(&buf, &mut offset, &metadata); + assert_eq!(vec.capacity(), 32); + assert_eq!(vec.len(), 32); + assert_eq!(vec.first_index(), 14); + assert_eq!(vec.last_index(), 13); + assert_eq!( + vec.iter().collect::>().as_slice(), + &[ + &[14_u8; 32], + &[15_u8; 32], + &[16_u8; 32], + &[17_u8; 32], + &[18_u8; 32], + &[19_u8; 32], + &[20_u8; 32], + &[21_u8; 32], + &[22_u8; 32], + &[23_u8; 32], + &[24_u8; 32], + &[25_u8; 32], + &[26_u8; 32], + &[27_u8; 32], + &[28_u8; 32], + &[29_u8; 32], + &[30_u8; 32], + &[31_u8; 32], + &[0_u8; 32], + &[1_u8; 32], + &[2_u8; 32], + &[3_u8; 32], + &[4_u8; 32], + &[5_u8; 32], + &[6_u8; 32], + &[7_u8; 32], + &[8_u8; 32], + &[9_u8; 32], + &[10_u8; 32], + &[11_u8; 32], + &[12_u8; 32], + &[13_u8; 32], + ] + ); + assert_eq!(offset, 1536); } } }