top | item 40508426

(no title)

bengl3rt | 1 year ago

I only read the paper (and the code in the paper) but not the complete source code, so maybe this would become clearer if I had, but...

Does Rust fundamentally guarantee that if you make a struct, its fields will lay out in memory in the order that you defined them? Can it be used to interact with APIs (really ABIs) who expect a C struct (or pointer to one)?

I think my main frustration with stuff like Go and Swift in this case is that their structs are not binary-compatible with C structs in this way because they rearrange things to be better aligned/packed/whatever.

discuss

order

Hemospectrum|1 year ago

> Does Rust fundamentally guarantee that if you make a struct, its fields will lay out in memory in the order that you defined them? Can it be used to interact with APIs (really ABIs) who expect a C struct (or pointer to one)?

You have to specify this behavior with #[repr(C)]. Otherwise, the compiler will rearrange fields to try to optimize packing and alignment.

Zanfa|1 year ago

> I think my main frustration with stuff like Go and Swift in this case is that their structs are not binary-compatible with C structs in this way because they rearrange things to be better aligned/packed/whatever.

If you need binary-compatibility with C structs in Swift, you can define them in a bridging header.

nXqd|1 year ago

in rust you can control the layout and alignment of fields in a struct with `#[repr(...)]`