Macro safe_transmute::try_copy[][src]

macro_rules! try_copy {
    ($res:expr) => { ... };
}

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

This macro, akin to try!(), will short-circuit certain errors by returning, namely guard condition and invalid value errors.

When the operation fails due to either an unaligned transmutation or an incompatible vector element transmutation target, the transmutation is reattempted by byte-copying (i.e. memcpy()ing) the input into a newly-allocated vector.

This expands into a single expression of type Cow<[T]>, where T is the target type.

Example

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

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