Convert C/C++ typedef or struct or types into Rust-native


I have a whole bunch of C/C++ header files that I want to convert to native data structures in Rust.
The headers lay out the structure of different data records.
So apart from what's in the headers, there will be no unknown variants.
The headers also contain union definitions which I'm sure is better replaced by Rust enum type.


enum Record {
    RecType1 { ..}
    RecType2 { ..}

I can use bindgen as a start but that'll insist on keeping certain aspects of the C/C++ definitions.
Apart from doing this by hand for hundreds of record types, is there any other crate that can get me close enough?


Do you just want a Rust type that could serve the same purpose, or do you actually need to use it in FFI? Rust enums are not FFI compatible with C tagged unions.

Hi, thanks for replying.
Just need the Rust types to serve the same purpose, no FFI necessary.

@alice thoughts?

Actually, they can have #[repr(...)] and they'll do. Type layout - The Rust Reference (

This looks like a text processing problem.
Maybe an awk script would be easier than doing it by hand?

Ok, I'd prefer to stick to Rust as I can do something like bindgen that runs the transform process if the source .h files have changed.

Are there any crates that can help with this text processing problem?
I was hoping nom would have been used already by someone for this use case of processing .h files.

By any chance, can Tree-sitter´ŻťIntroduction be used to parse C/C++'s header files, and the parsed content be made available to the language I'm working in (Rust)?
Then, can we morph that content into Rust-specific type definitions?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.