Commit db20a6cf authored by Hero Bird's avatar Hero Bird
Browse files

[pdsl_core] Use new to_be_bytes and from_be_bytes of primitives

Stabilized in Rust 1.32
parent 1f872d49
......@@ -16,8 +16,6 @@
//! Utilities to operate on byte or slices of bytes.
use core::mem::size_of;
/// Flips all bytes in the byte slice inplace.
fn invert_bytes(bytes: &mut [u8]) {
for byte in bytes.into_iter() {
......@@ -183,41 +181,6 @@ pub fn bytes_sub_bytes(lhs: &mut [u8], rhs: &[u8]) -> bool {
bytes_ops_bytes(lhs, rhs, u8::overflowing_sub)
}
macro_rules! primitives_impl {
( $prim:ty, $bytes_to_prim:ident, $prim_to_bytes:ident ) => {
/// Converts the byte array to the primitive number.
///
/// # Panics
///
/// If the byte slice does not match the number of byte
/// in the primitive.
pub fn $bytes_to_prim(bytes: &[u8; size_of::<$prim>()]) -> $prim {
let mut res = 0;
const N_BYTES: usize = size_of::<$prim>();
const N_BITS: usize = N_BYTES * 8;
for i in 0..N_BYTES {
res |= (bytes[i] as $prim) << (N_BITS - ((i + 1) * 8));
}
res
}
/// Converts the primitive number to a byte array.
pub fn $prim_to_bytes(val: $prim) -> [u8; size_of::<$prim>()] {
const N_BYTES: usize = size_of::<$prim>();
const N_BITS: usize = N_BYTES * 8;
let mut buf = [0x0; N_BYTES];
for i in 0..N_BYTES {
buf[i] = ((val >> (N_BITS - ((i + 1) * 8))) & 0xFF) as u8
}
buf
}
};
}
primitives_impl!(u32, bytes4_to_u32, u32_to_bytes4);
primitives_impl!(u64, bytes8_to_u64, u64_to_bytes8);
primitives_impl!(u128, bytes16_to_u128, u128_to_bytes16);
#[cfg(all(test, feature = "std"))]
mod tests {
use super::*;
......@@ -353,74 +316,4 @@ mod tests {
);
})
}
#[test]
fn u32_and_bytes_conv() {
run_test(|| {
fn test_for(val: u32, bytes: [u8; 4]) {
assert_eq!(bytes4_to_u32(&u32_to_bytes4(val)), val);
assert_eq!(u32_to_bytes4(bytes4_to_u32(&bytes)), bytes);
assert_eq!(u32_to_bytes4(val), bytes);
}
test_for(
0x00_00_00_00,
[0x00, 0x00, 0x00, 0x00]
);
test_for(
0xFF_FF_FF_FF,
[0xFF, 0xFF, 0xFF, 0xFF]
);
test_for(
0x00_00_00_01,
[0x00, 0x00, 0x00, 0x01]
);
test_for(
0x12_34_56_78,
[0x12, 0x34, 0x56, 0x78]
);
})
}
#[test]
fn u64_and_bytes_conv() {
run_test(|| {
fn test_for(val: u64, bytes: [u8; 8]) {
assert_eq!(bytes8_to_u64(&u64_to_bytes8(val)), val);
assert_eq!(u64_to_bytes8(bytes8_to_u64(&bytes)), bytes);
assert_eq!(u64_to_bytes8(val), bytes);
}
// Test for 0
test_for(
0x00_00_00_00_00_00_00_00,
[
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
]
);
// Test for MAX
test_for(
0xFF_FF_FF_FF_FF_FF_FF_FF,
[
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
]
);
// Test for 1
test_for(
0x00_00_00_00_00_00_00_01,
[
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
]
);
// Test for unique bytes
test_for(
0x12_34_56_78_9A_BC_DE_F0,
[
0x12, 0x34, 0x56, 0x78,
0x9A, 0xBC, 0xDE, 0xF0,
]
);
})
}
}
......@@ -118,8 +118,8 @@ impl KeyDiff {
if self.as_bytes().into_iter().take(KEY_BYTES - U32_BYTES).any(|&byte| byte != 0x0) {
return None
}
let value = byte_utils::bytes4_to_u32(
byte_utils::slice4_as_array4(&self.as_bytes()[(KEY_BYTES - U32_BYTES)..KEY_BYTES])
let value = u32::from_be_bytes(
*byte_utils::slice4_as_array4(&self.as_bytes()[(KEY_BYTES - U32_BYTES)..KEY_BYTES])
.unwrap()
);
Some(value)
......@@ -134,8 +134,8 @@ impl KeyDiff {
if self.as_bytes().into_iter().take(KEY_BYTES - U64_BYTES).any(|&byte| byte != 0x0) {
return None
}
let value = byte_utils::bytes8_to_u64(
byte_utils::slice8_as_array8(&self.as_bytes()[(KEY_BYTES - U64_BYTES)..KEY_BYTES])
let value = u64::from_be_bytes(
*byte_utils::slice8_as_array8(&self.as_bytes()[(KEY_BYTES - U64_BYTES)..KEY_BYTES])
.unwrap()
);
Some(value)
......@@ -150,8 +150,8 @@ impl KeyDiff {
if self.as_bytes().into_iter().take(KEY_BYTES - U128_BYTES).any(|&byte| byte != 0x0) {
return None
}
let value = byte_utils::bytes16_to_u128(
byte_utils::slice16_as_array16(&self.as_bytes()[(KEY_BYTES - U128_BYTES)..KEY_BYTES])
let value = u128::from_be_bytes(
*byte_utils::slice16_as_array16(&self.as_bytes()[(KEY_BYTES - U128_BYTES)..KEY_BYTES])
.unwrap()
);
Some(value)
......@@ -159,7 +159,7 @@ impl KeyDiff {
}
macro_rules! impl_add_sub_for_key {
( $prim:ty, $prim_to_bytes_fn:ident ) => {
( $prim:ty ) => {
impl core::ops::Add<$prim> for Key {
type Output = Self;
......@@ -174,7 +174,7 @@ macro_rules! impl_add_sub_for_key {
fn add_assign(&mut self, rhs: $prim) {
let ovfl = byte_utils::bytes_add_bytes(
self.as_bytes_mut(),
&byte_utils::$prim_to_bytes_fn(rhs)
&(rhs.to_be_bytes())
);
if ovfl {
log::warn!(
......@@ -201,7 +201,7 @@ macro_rules! impl_add_sub_for_key {
fn sub_assign(&mut self, rhs: $prim) {
let ovfl = byte_utils::bytes_sub_bytes(
self.as_bytes_mut(),
&byte_utils::$prim_to_bytes_fn(rhs)
&rhs.to_be_bytes()
);
if ovfl {
log::warn!(
......@@ -216,9 +216,9 @@ macro_rules! impl_add_sub_for_key {
};
}
impl_add_sub_for_key!(u32, u32_to_bytes4);
impl_add_sub_for_key!(u64, u64_to_bytes8);
impl_add_sub_for_key!(u128, u128_to_bytes16);
impl_add_sub_for_key!(u32);
impl_add_sub_for_key!(u64);
impl_add_sub_for_key!(u128);
#[cfg(all(test, feature = "test-env"))]
mod tests {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment