Skip to main content

osom_lib_arrays/const_helpers/
endianness_helpers.rs

1use osom_lib_macros::debug_check_or_release_hint;
2
3/// Converts slice with offset to a little-endian u64.
4///
5/// # Safety
6///
7/// This function does not verify the out-of-bounds access.
8/// It is up to the caller to ensure that the range is valid.
9///
10/// # Panics
11///
12/// It will panic on out-of-bounds access, but only in debug mode.
13#[allow(private_bounds)]
14#[inline(always)]
15#[must_use]
16pub const unsafe fn from_le_const_u64(slice: &[u8], start: usize) -> u64 {
17    debug_check_or_release_hint!(slice.len() >= start + size_of::<u64>());
18    let array_ptr = unsafe { slice.as_ptr().add(start) }.cast::<[u8; size_of::<u64>()]>();
19    u64::from_le_bytes(unsafe { *array_ptr })
20}
21
22/// Converts slice with offset to a big-endian u32.
23///
24/// # Safety
25///
26/// This function does not verify the out-of-bounds access.
27/// It is up to the caller to ensure that the range is valid.
28///
29/// # Panics
30///
31/// It will panic on out-of-bounds access, but only in debug mode.
32#[allow(private_bounds)]
33#[inline(always)]
34#[must_use]
35pub const unsafe fn from_be_const_u32(slice: &[u8], start: usize) -> u32 {
36    debug_check_or_release_hint!(slice.len() >= start + size_of::<u32>());
37    let array_ptr = unsafe { slice.as_ptr().add(start) }.cast::<[u8; size_of::<u32>()]>();
38    u32::from_be_bytes(unsafe { *array_ptr })
39}