[][src]Function safe_transmute::pod::transmute_pod_vec

pub unsafe fn transmute_pod_vec<T: PodTransmutable, G: Guard>(
    bytes: Vec<u8>
) -> Result<Vec<T>, Error>

Transform a byte vector into a vector of POD.

The resulting vec will reuse the allocated byte buffer when possible, and should have at least enough bytes to fill a single instance of a type. Extraneous data is ignored.

Errors

An error is returned in one of the following situations:

Safety

This function invokes undefined behavior if the data does not have a memory alignment compatible with T. If this cannot be ensured, you will have to make a copy of the data, or change how it was originally made.

Examples

// Little-endian
unsafe {
    assert_eq!(transmute_pod_vec::<u16, SingleManyGuard>(vec![0x00, 0x01, 0x00, 0x02])?,
           vec![0x0100, 0x0200]);
    assert_eq!(transmute_pod_vec::<u32, SingleManyGuard>(vec![0x04, 0x00, 0x00, 0x00, 0xED])?,
           vec![0x0000_0004]);

    assert!(transmute_pod_vec::<i16, SingleManyGuard>(vec![0xED]).is_err());
}