Function read_u64_ne

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

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

§Safety

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

§Arguments

  • bytes: the byte array reference

returns: u64

§Examples

use hyper_byte::read_u64_ne;

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


pub fn read_u64_safe_ne(array: &[u8], index: &mut usize) -> u64 {
    // "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_u64_ne(&array[*index..(*index+8)]) };
    *index += 8;
    third_value
}