mozilla / cbindgen

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

Add support for std::mem::offset_of #984

Open basile-henry opened 1 month ago

basile-henry commented 1 month ago

It looks like std::mem::offset_of isn't supported yet

src/lib.rs:

struct A {
    a: bool,
}

pub const offset: usize = std::mem::offset_of!(A, a);
$ cbindgen --config template.toml --crate repro --output my_header.h
WARN: Skip repro::offset - (Unsupported expression. Macro(ExprMacro { attrs: [], mac: Macro { path: Path { leading_colon: None, segments: [PathSegment { ident: Ident(std), arguments: None }, Colon2, PathSegment { ident: Ident(mem), arguments: None }, Colon2, PathSegment { ident: Ident(offset_of), arguments: None }] }, bang_token: Bang, delimiter: Paren(Paren), tokens: TokenStream [Ident { sym: A }, Punct { char: ',', spacing: Alone }, Ident { sym: a }] } }))
emilio commented 2 weeks ago

This seems generally hard because we don't have compiler information. We could try to translate it to an offsetof in C / C++ perhaps tho?