Function lazy_bytes_cast::to_bytes

source ·
pub const fn to_bytes<T: Pod, const N: usize>(val: &T) -> [u8; N]
Expand description

Reads N bytes from Pod object by performing memcpy

Usage

use lazy_bytes_cast::to_bytes;

const INPUT: [u8; 4] = [123, 25, 99, 250];
static BYTES: [u8; 4] = to_bytes(&u32::from_ne_bytes(INPUT));
assert_eq!(BYTES, INPUT);

static HALF: [u8; 2] = to_bytes(&u32::from_ne_bytes(INPUT));
assert_eq!(HALF, &INPUT[..2]);

Restrictions

Compilation fails if val doesn’t have enough bytes to read from.

use lazy_bytes_cast::to_bytes;

static BYTES: [u8; 5] = to_bytes(&0u32);