split bytes utils into unit

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-03 00:54:36 +00:00
parent af81baae44
commit 3b9fba233c
4 changed files with 45 additions and 31 deletions

29
src/core/utils/bytes.rs Normal file
View file

@ -0,0 +1,29 @@
use crate::Result;
#[inline]
#[must_use]
pub fn increment(old: Option<&[u8]>) -> [u8; 8] {
old.map(TryInto::try_into)
.map_or(0_u64, |val| val.map_or(0_u64, u64::from_be_bytes))
.wrapping_add(1)
.to_be_bytes()
}
/// Parses the big-endian bytes into an u64.
#[inline]
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64> {
let array: [u8; 8] = bytes.try_into()?;
Ok(u64_from_u8x8(array))
}
/// Parses the 8 big-endian bytes into an u64.
#[inline]
#[must_use]
pub fn u64_from_u8(bytes: &[u8]) -> u64 {
let bytes: &[u8; 8] = bytes.try_into().expect("must slice at least 8 bytes");
u64_from_u8x8(*bytes)
}
#[inline]
#[must_use]
pub fn u64_from_u8x8(bytes: [u8; 8]) -> u64 { u64::from_be_bytes(bytes) }