Skip to content

Commit

Permalink
update from_raw_parts to use from_raw_parts_with_size
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Oct 27, 2024
1 parent 6135656 commit 1447f50
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
20 changes: 6 additions & 14 deletions src/sequence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,7 @@ impl<'a> Sequence<'a> {
/// and between `1` and `32` bytes (inclusive) if `is_signed` is `false`.
pub fn from_raw_parts<T>(slice: &'a [T], is_signed: bool) -> Self {
let element_size = core::mem::size_of::<T>();
if is_signed {
assert!(element_size > 0);
assert!(element_size <= 16);
} else {
assert!(element_size > 0);
assert!(element_size <= 32);
}
let len = std::mem::size_of_val(slice);
let data_slice = unsafe { core::slice::from_raw_parts(slice.as_ptr() as *const u8, len) };
Sequence {
data_slice,
element_size,
is_signed,
}
Self::from_raw_parts_with_size(slice, element_size, is_signed)
}

/// Converts a slice of any type to a Sequence by calling `from_raw_parts` on it.
Expand All @@ -114,6 +101,11 @@ impl<'a> Sequence<'a> {
assert!(element_size <= 32);
}
let len = std::mem::size_of_val(slice);
assert_eq!(
len % element_size,
0,
"raw data length should be a multiple of element size"
);
let data_slice = unsafe { core::slice::from_raw_parts(slice.as_ptr() as *const u8, len) };
Sequence {
data_slice,
Expand Down
2 changes: 1 addition & 1 deletion src/sequence/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn we_can_convert_a_slice_of_scalars_to_a_sequence_with_correct_data() {
#[test]
fn we_can_convert_a_slice_of_fixed_size_binary_to_a_sequence_with_correct_data() {
let element_size = 4;
let s = vec![
let s = [
[0x01u8, 0x02u8, 0x03u8, 0x04u8],
[0x05u8, 0x06u8, 0x07u8, 0x08u8],
[0x09u8, 0x0Au8, 0x0Bu8, 0x0Cu8],
Expand Down

0 comments on commit 1447f50

Please sign in to comment.