unsafe_byteable_transmute

Macro unsafe_byteable_transmute 

Source
macro_rules! unsafe_byteable_transmute {
    ($($type:ty),+) => { ... };
}
Expand description

Implements Byteable for one or more types using transmute.

This macro provides a quick way to implement Byteable for types that can be safely transmuted to/from byte arrays. This is useful for #[repr(C)] or #[repr(transparent)] types.

§Safety

This macro uses unsafe code (std::mem::transmute). You must ensure:

  • The type has a well-defined memory layout
  • All byte patterns are valid for the type
  • The type has no padding bytes with uninitialized memory

§Examples

use byteable::{Byteable, unsafe_byteable_transmute};

#[repr(transparent)]
struct MyU32(u32);

unsafe_byteable_transmute!(MyU32);

let value = MyU32(0x12345678);
let bytes = value.as_byte_array();

Multiple types can be implemented at once:

use byteable::unsafe_byteable_transmute;

#[repr(transparent)]
struct TypeA(u16);

#[repr(transparent)]
struct TypeB(u32);

unsafe_byteable_transmute!(TypeA, TypeB);