mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.27k stars 294 forks source link

Generate C packed struct #952

Closed flaviojs closed 1 month ago

flaviojs commented 1 month ago

I can't get cbindgen to generate packed structs.

I was expecting something like:

#[repr(C, packed)]
pub struct repr_c_packed  {
    pub a: u8,
    pub b: u16,
    pub c: u32,
}

to generate the C code:

struct repr_c_packed {
  uint8_t a;
  uint16_t b;
  uint32_t c;
} __attribute__((__packed__));

What is the correct way to generate a packed struct? (if needed, I'm willing to implement the feature in cbindgen)

emilio commented 1 month ago

We have support for this (#431), is that not working? You need to set the layout.packed config tho.

flaviojs commented 1 month ago

When I did not see the struct details I was expecting a warning message about the type, like other skipped types. Searching the issues and reading the docs did not enlighten me about layout.packed.

Now I know how to generated the struct details. It works, thanks. :+1: