Why does transmute reorder bytes when transmuting from a byte array to a tuple of bytes and byte arrays?

fn main() {
    let buffer: [u8; 16] = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ];
    let (a, b, c, d, e): (u8, u8, u8, [u8; 4], [u8; 9]) = unsafe { core::mem::transmute(buffer) };
    println!("{a:#x}");
    println!("{b:#x}");
    println!("{c:#x}");
    println!("{d:#x?}");
    println!("{e:#x?}");
}

Rust playground

Output:

0x4
0x5
0x6
[
    0x0,
    0x1,
    0x2,
    0x3,
]
[
    0x7,
    0x8,
    0x9,
    0xa,
    0xb,
    0xc,
    0xd,
    0xe,
    0xf,
]

Why don’t a, b, and c get the values 0x00, 0x01, and 0x02 (respectively)? Why do those values instead go into d?

>Solution :

AFAIK tuples are layed out in memory similarly to structs, but those don’t necessarily have memory layout with fields being in the same order as in struct definition. You can try to use repr(C) attribute onto a custom struct to preserve this order

The Rustonomicon: Alternative representations

#[repr(C)]
struct DecodedBytes(u8, u8, u8, [u8; 4], [u8; 9]);

fn main() {

    let buffer: [u8; 16] = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ];
   
    let DecodedBytes(a, b, c, d, e)  = unsafe { core::mem::transmute(buffer) };
    println!("{a:#x}");
    println!("{b:#x}");
    println!("{c:#x}");
    println!("{d:#x?}");
    println!("{e:#x?}");
}

0x0
0x1
0x2
[
0x3,
0x4,
0x5,
0x6,
]
[
0x7,
0x8,
0x9,
0xa,
0xb,
0xc,
0xd,
0xe,
0xf,
]

Leave a Reply