Function safe_transmute::to_bytes::transmute_to_bytes_many_unchecked_mut[][src]

pub unsafe fn transmute_to_bytes_many_unchecked_mut<S>(
    from: &mut [S]
) -> &mut [u8]

Transmute a mutable slice of arbitrary types into a mutable slice of their bytes.

Safety

This function is very ill advised, since it can be exploited to break invariants of the source type. Any modification that leaves the data in an inconsistent state with respect to S is undefined behavior.

Examples

Some u16s:

unsafe {
    // Little-endian
    assert_eq!(transmute_to_bytes_mut(&mut [0x0123u16, 0x4567u16]),
               &[0x23, 0x01, 0x67, 0x45]);
}

An arbitrary type:

#[repr(C)]
#[derive(Debug, Eq, PartialEq)]
struct Gene {
    x1: u8,
    x2: u8,
}

let mut genes = [Gene {
                     x1: 0x42,
                     x2: 0x69,
                 },
                 Gene {
                     x1: 0x12,
                     x2: 0x48,
                 }];

unsafe {
    let gene_data = transmute_to_bytes_many_unchecked_mut(&mut genes);
    assert_eq!(gene_data, &mut [0x42, 0x69, 0x12, 0x48]);

    gene_data[0] = 0xB0;
    gene_data[3] = 0x0B;
}

assert_eq!(genes, [Gene {
                       x1: 0xB0,
                       x2: 0x69,
                   },
                   Gene {
                       x1: 0x12,
                       x2: 0x0B,
                   }]);