diff options
author | Orne Brocaar <info@brocaar.com> | 2020-01-13 08:49:49 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-01-13 14:22:31 +0100 |
commit | 96d8961382122b7edc4a315d54e5b50d88c35091 (patch) | |
tree | 811d59ab901a64962f2252f7cbcb8ef51b161f3d | |
parent | 7941b9107aa410e23cb172991085f1b1a0e83b07 (diff) |
Add how to deal with bindgen padding fields.
-rw-r--r-- | book/src/faq.md | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/book/src/faq.md b/book/src/faq.md index 4f4bd820..ed5f284b 100644 --- a/book/src/faq.md +++ b/book/src/faq.md @@ -64,3 +64,34 @@ As far as generating opaque blobs of bytes with the correct size and alignment, transitively contain STL things. We generally recommend marking `std::.*` as opaque, and then whitelisting only the specific things you need from the library you're binding to that is pulling in STL headers. + +### How to deal with bindgen generated padding fields? + +Depending the architecture, toolchain versions and source struct, it is +possible that bindgen will generate padding fields named `__bindgen_padding_N`. +As these fields might be present when compiling for one architecture but not +for an other, you should not initialize these fields manually when initializing +the struct. Instead, use the `Default` trait. You can either enable this when +constructing the `Builder` using the `derive_default` method, or you can +implement this per struct using: + +```rust +impl Default for SRC_DATA { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +``` + +This makes it possible to initialize `SRC_DATA` by: + +```rust +SRC_DATA { + field_a: "foo", + field_b: "bar", + ..Default::default() +} +``` + +In the case bindgen generates a padding field, then this field will +be automatically initialized by `..Default::default()`. |