Skip to content

Commit

Permalink
Implement ByteValued for i128/u128
Browse files Browse the repository at this point in the history
The only interesting thing in this commit is adjusting the unit test
that tests these implementations, as u128/i128 can be aligned to 8 byte
boundaries on x86. So only check that reading fails for values
misaligned by 1 through 7 bytes.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
  • Loading branch information
roypat authored and jiangliu committed Jun 28, 2023
1 parent a3cc708 commit d0bcbb3
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,13 @@ byte_valued_type!(u8);
byte_valued_type!(u16);
byte_valued_type!(u32);
byte_valued_type!(u64);
byte_valued_type!(u128);
byte_valued_type!(usize);
byte_valued_type!(i8);
byte_valued_type!(i16);
byte_valued_type!(i32);
byte_valued_type!(i64);
byte_valued_type!(i128);
byte_valued_type!(isize);

/// A trait used to identify types which can be accessed atomically by proxy.
Expand Down Expand Up @@ -362,7 +364,7 @@ pub(crate) mod tests {
where
T: ByteValued + PartialEq + Debug + Default,
{
let mut data = [0u8; 32];
let mut data = [0u8; 48];
let pre_len = {
let (pre, _, _) = unsafe { data.align_to::<T>() };
pre.len()
Expand All @@ -377,7 +379,7 @@ pub(crate) mod tests {
assert_eq!(val.as_mut_slice(), aligned_data);
}
}
for i in 1..size_of::<T>() {
for i in 1..size_of::<T>().min(align_of::<T>()) {
let begin = pre_len + i;
let end = begin + size_of::<T>();
let unaligned_data = &mut data[begin..end];
Expand All @@ -401,11 +403,13 @@ pub(crate) mod tests {
check_byte_valued_type::<u16>();
check_byte_valued_type::<u32>();
check_byte_valued_type::<u64>();
check_byte_valued_type::<u128>();
check_byte_valued_type::<usize>();
check_byte_valued_type::<i8>();
check_byte_valued_type::<i16>();
check_byte_valued_type::<i32>();
check_byte_valued_type::<i64>();
check_byte_valued_type::<i128>();
check_byte_valued_type::<isize>();
}

Expand Down

0 comments on commit d0bcbb3

Please sign in to comment.