read_f32_be

Function read_f32_be 

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

Unsafe, near zero cost transmutation of a byte array slice into a 32-bit floating point using big-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: f32

§Examples

use hyper_byte::read_f32_be;

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


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