Function safe_transmute::to_bytes::transmute_one_to_bytes_mut
source · pub fn transmute_one_to_bytes_mut<S: TriviallyTransmutable>(
from: &mut S
) -> &mut [u8] ⓘ
Expand description
Transmute a single instance of a trivially transmutable type into a slice of its bytes.
§Examples
An u32
:
// Little-endian
assert_eq!(transmute_one_to_bytes_mut(&mut 0x0123_4567),
&mut [0x67, 0x45, 0x23, 0x01]);
An arbitrary type:
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Gene {
x1: u8,
x2: u8,
}
unsafe impl TriviallyTransmutable for Gene {}
let mut gene = Gene {
x1: 0x42,
x2: 0x69,
};
{
let gene_data = transmute_one_to_bytes_mut(&mut gene);
assert_eq!(gene_data, &mut [0x42, 0x69]);
gene_data[0] = 0xB0;
}
assert_eq!(gene, Gene {
x1: 0xB0,
x2: 0x69,
});