Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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,
]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading