read_i16_le

Function read_i16_le 

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

Unsafe, near zero cost transmutation of a byte array slice into a signed 16-bit integer using little-endianness.

§Safety

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

§Arguments

  • bytes: the byte array reference

returns: i16

§Examples

use hyper_byte::read_i16_le;

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


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