Macro safe_transmute::try_copy_unchecked

source ·
macro_rules! try_copy_unchecked {
    ($res:expr) => { ... };
}
Expand description

Retrieve the result of a non-trivial transmutation, copying the data if it could not be safely performed due to memory alignment constraints.

Equivalent to try_copy!(), except for not checking that the target type is trivially transmutable.

§Safety

The source data needs to correspond to a valid contiguous sequence of T values.

§Example

let bytes = &[0x00, 0x01, 0x12, 0x34,
              0x00]; // 1 spare byte
unsafe {
    let words = try_copy_unchecked!(transmute_many::<u16, SingleManyGuard>(bytes));

    assert_eq!(*words,
               [u16::from_be(0x0001), u16::from_be(0x1234)]);
}