Function read_u32_ne

Source
pub unsafe fn read_u32_ne(bytes: &[u8]) -> u32
Expand description

Unsafe, near zero cost transmutation of a byte array slice into an unsigned 32-bit integer using little-endianness.

§Safety

To make it “safe” and does not cause memory errors, you must ensure the input has at least 4 bytes prior to calling this.

§Arguments

  • bytes: the byte array reference

returns: u32

§Examples

use hyper_byte::read_u32_ne;

let slice = [0u8; 16];
// PERFECTLY SAFE
let first_value = unsafe { read_u32_ne(&slice[0..4]) };
// NOT SAFE
let second_value = unsafe { read_u32_ne(&slice[9..9]) };


pub fn read_u32_safe_ne(array: &[u8], index: &mut usize) -> u32 {
    // "SAFE" because even if the array is not of this length,
    // it will still at least panic and not cause undefined behaviour
    let third_value = unsafe { read_u32_ne(&array[*index..(*index+4)]) };
    *index += 4;
    third_value
}