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

Compile time layout tests #2787

Closed GKFX closed 3 months ago

GKFX commented 3 months ago

This commit emits layout tests as compile-time assertions, rather than as unit tests. This allows them to be run in situations where running unit tests is unfeasible, i.e. cross-compiled code and nostd targets.

Fixes #2786. @ojeda - I noticed you tagged the feature request for Rust-for-Linux.

emilio commented 3 months ago

This looks great, though I think it's a requirement to make it the default to be able to tell how the values differ... Can we use format!() in the message or something along those lines?

emilio commented 3 months ago

(or does the assert macro print that already?)

pvdrz commented 3 months ago

There's no way to print such thing on const context AFAIK

emilio commented 3 months ago

I haven't been able to play with this yet but maybe we can abuse slice indexing to at least print the differences between actual and expected? E.g for [0u8][X - Y] or something like that which would panic if X was different to Y, and I would expect the compiler to print the bad index?

pvdrz commented 3 months ago

oh that's so hacky... I love it!

GKFX commented 3 months ago

Using slice indexing works. I've put the string as the expression in the array rather than 0u8 so it's visible.