Skip to main content

pod_read_unaligned

Function pod_read_unaligned 

Source
pub const fn pod_read_unaligned<T: AnyBitPattern>(bytes: &[u8]) -> T
Expand description

Reads a T out of a byte slice.

§Panics

This panics if size_of::<T>() != bytes.len()

§Example

use constmuck::{pod_read_unaligned, AnyBitPattern};

#[repr(C)]
#[derive(Debug, PartialEq, Copy, Clone, AnyBitPattern)]
struct Foo(u16, u16);

const FOO: Foo = {
    let number = u32::from_be_bytes([0xDe, 0x11, 0x0_B, 0x0b]);
    pod_read_unaligned(&number.to_ne_bytes())
};

assert_eq!(FOO, Foo(0xB0b, 0xDe11));