Inspiration
A few months ago, I was toying with the idea of rewriting parts of a medium-sized C project in Rust. Wanting to do this is the intended use case for rust-bindgen, an official rust toolchain tool for generating Rust bindings for C and C++ code. I quickly took note of some problems and personal frustrations with it. Namely, it depends on libclang to parse C source, which is a very large dependency that can be annoying to install and cannot be assumed to be present everywhere a C or Rust project is being built. Also, rust-bindgen was unable to automatically convert certain constant C macros to Rust constants, which I went over in detail in the demo video.
What it does
minibindgen is a dependency free, feature-focused replacement for rust-bindgen for C. The goal was to fix the feature limitations of bindgen while also breaking free from dependency on libclang, and ideally on any external crates at all. minibindgen was built, of course, in Rust, and without the of any external dependencies from cargo or otherwise.
Challenges I ran into
Parsing C is tricky. The C standard conceptually lays out the specific stages C source code goes through as it is processed, but there is a lot of nuance to accurately implementing each of these. I found myself having to spend a lot of time trying out my code with strange input to make sure there wasn't a subtle bug.
Accomplishments that I'm proud of
minibindgen in its current state has code to tokenize all C syntax accurately, and to mostly function in place of the C preprocessor. The proof of concept shown the video is able to solve the problem I was initially inspired by, and there is enough framework code laid out that it could relatively easily be expanded from this point to replace most of bindgen's functionality for binding to C.
What I learned
There's a reason why everyone uses a library to parse C code instead of trying to do it themselves. It's kind of a nightmare at first glance. And second glance. But ultimately, it's a specification that humans designed then designed code to read, multiple times, so it's an understandable and solvable problem. Also, as I learn every time I write something in Rust, I love Rust.
What's next for minibindgen
If I find the time, I intend to make it as much of a complete replacement for bindgen for C as possible, and maybe use it for Rust-C interop in some projects of my own.
Log in or sign up for Devpost to join the conversation.