Blog coding article

Binding with bindgen


Binding with bindgen

Published on 3 min read
bindgen icon
A Rust FFI bindings to C (and some C++) generator.

    I bet you know that at Ferrous Systems we are big proponents of open source. You are also probably aware that Ferrous Systems is a leading name in Rust training. But – did you know that we also consult on a range of client projects? Some of these projects include contributing to OSS projects that our clients depend on. This is why we were asked to work on the bindgen project (also known as rust-bindgen).

    bindgen is a tool to generate Rust FFI bindings, mainly to C (and sometimes C++) libraries. It is a useful tool for facilitating writing code in Rust while not having to rewrite the existing libraries written in C (or C++). If you would like to read more about bindgen, then you can check out bindgen's documentation. This post is a quick update on what we’ve been up to with bindgen, with the help of the community and the project’s maintainers and contributors.

    Before we jump into features and fixes, there is a lot of work that has gone into pruning and triaging of the existing issues. Since we started in July 2022, we have merged about 70 pull requests. A staggering 66 just from my colleague Christian (yea, I know!)! During the same time, we have also done two releases - v0.63.0 and v0.64.0. We have also closed a bunch of issues as a result of continuously triaging. I must mention that without the help of maintainers, especially Emilio Cobos Álvarez, and contributors, this would not have been possible. Emilio has been incredibly generous with his time and support.

    Some prominent issues and features (in my humble opinion)

    I do not want to downplay other issues, bugfixes, features but I also cannot add just a large list of issues and PRs here. This is not how PR (public relations) work, right?

    bindgen as a workspace #2284

    This change split the project into a cargo workspace. The main crates within this workspace being bindgen and bindgen-cli meaning that using bindgen in a build script no longer requires pulling CLI related dependencies. You can see the new Cargo.toml here.

    Generate C code to export static inline functions #1090

    What a journey this issue has had. A crucial one but open since 2017. Like most of the issues in bindgen (or just collaborative software in general), this one also had a consensus issue. The pull request for this issue Generate extern wrappers for inlined functions #2335 took about four months of work and discussion. In short, this change allows a new way to generate FFI for C's static inline functions. The point to note is that this feature, at the time of this writing, is experimental. Having features behind an experimental flag first may be a longer route than usual. But we try to not remove or break old functionality and carefully introduce new ones.

    The reason this issue is a big deal is because our solution also generates a C source file that defines external wrapper functions. It seems that some of the more complex features need this kind of dance. You can read Christian's example on How to handle static inline functions.

    Thank you

    We are happy that we have been able to contribute to this project, but this would not have been possible without the maintainers of the project and their help. This is especially true for Emilio. Also, many thanks to the contributors of the project.