4 ms·
That's right, you can pack things with a pragma directive in C and C++ for some compilers. Rust can take it a bit further. In rust, fields must be aligned to a
by PartiallyTyped 3y ago
That's right, you can pack things with a pragma directive in C and C++ for some compilers.
Rust can take it a bit further. In rust, fields must be aligned to a multiple of their size, and structs/tuples must be aligned to a multiple of their largest element. This combination makes it very easy to handle memory offsets, or reusing the same representation in a way that reduces memory usage (these are called niches)[1].
This is why dynamic linking is a bit more complex in rust. Alternatively you can offer a C bridge via `extern "c"`. C will act as a bridge between rust and different languages, and at that stage you need to take care of ordering and padding.
[1] https://doc.rust-lang.org/nomicon/repr-rust.html https://doc.rust-lang.org/nomicon/repr-rust.html