#[inline]
pub const fn sign_ext_byte(is_positive: bool) -> u8 {
match is_positive {
true => 0x00,
false => 0xFF,
}
}
#[inline]
pub fn extend_bytes<const N: usize, const M: usize>(dst: &mut [u8; N], src: &[u8; M]) {
debug_assert!(N > M);
let offset = cfg!(target_endian = "big")
.then(|| usize::abs_diff(N, M))
.unwrap_or(0);
dst[offset..][..M].copy_from_slice(src);
}
#[inline]
pub fn truncate_bytes<const N: usize, const M: usize>(dst: &mut [u8; N], src: &[u8; M]) {
debug_assert!(N < M);
let offset = cfg!(target_endian = "big")
.then(|| usize::abs_diff(N, M))
.unwrap_or(0);
dst[..].copy_from_slice(&src[offset..][..N]);
}
#[inline]
pub fn reverse_bytes<const N: usize>(array: [u8; N]) -> [u8; N] {
let mut array = array;
array.reverse();
array
}
#[inline]
pub fn le_bytes_to_ne<const N: usize>(array: [u8; N]) -> [u8; N] {
match cfg!(target_endian = "little") {
true => array,
false => reverse_bytes(array),
}
}
#[inline]
pub fn ne_bytes_to_le<const N: usize>(array: [u8; N]) -> [u8; N] {
match cfg!(target_endian = "little") {
true => array,
false => reverse_bytes(array),
}
}
#[inline]
pub fn be_bytes_to_ne<const N: usize>(array: [u8; N]) -> [u8; N] {
match cfg!(target_endian = "big") {
true => array,
false => reverse_bytes(array),
}
}
#[inline]
pub fn ne_bytes_to_be<const N: usize>(array: [u8; N]) -> [u8; N] {
match cfg!(target_endian = "big") {
true => array,
false => reverse_bytes(array),
}
}