rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.23k stars 679 forks source link

Feature request: compile-time layout tests #2786

Closed GKFX closed 3 months ago

GKFX commented 3 months ago

Bindgen currently emits unit tests to check layout. However, these are quite difficult to run for a cross-compiled target as there may be no way to run code on the target as part of the build process. Targets not supporting std, i.e. embedded, are harder again to run the tests on.

With the stabilization of core::mem::offset_of it should now be possible to convert the layout tests into compile-time assertions like below. This would make it much easier for embedded users to run these tests.

const _LAYOUT_TEST_S: () = {
    assert!(::core::mem::size_of::<S>() == 4);
    assert!(::core::mem::align_of::<S>() == 4);
    assert!(::core::mem::offset_of!(S, field1) == 0);
    // etc.
};