read_usize_be

Function read_usize_be 

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

Unsafe, near zero cost transmutation of a byte array slice into an usize integer using big-endianness.

§Safety

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

§Arguments

  • bytes: the byte array reference

returns: usize

§Examples

use hyper_byte::read_usize_be;

let slice = [0u8; 16];
// PERFECTLY SAFE
let first_value = unsafe { read_usize_be(&slice[0..size_of::<usize>()]) };
// NOT SAFE
let second_value = unsafe { read_usize_be(&slice[9..9]) };


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