CGlue 0.2 is here!

Published: Nov. 26, 2021, 2:37 a.m.

Beautiful things happen, when you let the type system do the work for you. CGlue has reached a very important milestone, making Rust to C/C++ interop not just possible, but also seamless!

Recap

CGlue aims to provide a friction-less way for traits to be ABI-safe, using Rust's type system to ensure code validity as much as possible. With such safety you are then able to either build dynamically loadable Rust plugins, or go an extra mile and interop with other languages. As planned just over 4 months ago, my plan was to overhaul the project to allow for better usage from C/C++, as it was not intuitive at best. The end result is very exciting.

Results

Everything planned in the previous post was achieved, and more! Here are the highlights:

  • Usage of generated code has been simplified.

  • Ergonomic C++ wrappers can be auto-generated. Allowing C++ users to use Rust plugins as well as create their own plugins.

  • Same thing with C. Inline function wrappers are being generated to make usage experience be as intuitive as possible.

  • Integration with abi_stable for API/ABI mismatch checking. Note that CGlue will not work with crates.io version just yet, as a certain patch needs to be published first. In the meantime, this can be solved by specifying the upstream repo in cargo patch section.

  • With Rust's specialization feature, CGlue trait groups can be auto-implemented, and thus, to simplify usage, a unstable feature flag was added. If anyone knows how to implement something like try_default in stable Rust, or at least with min_specialization, please let me know, because it would help the project a lot!

  • Add vtable-only functions, in cases where CGlue can not perform a trait implementation within the constraints, but with slight type modifications would be very useful for foreign language users.

The full changelog is available in the repo.

Future

I am pretty happy with existing feature set, but there is more that can be done. For instance, cglue-bindgen is very fragile, and moving as much of its functionality into cbindgen would be desirable. In addition, I have begun to think what needs to be done to support other languages/environments as well, such as Python and WASM. Finally, a lot of CGlue's automatic type wrapping could come in handy for regular functions as well. My aim would be to split the code generator a bit further to support wrapping individual functions, as well as regular struct implementations.

Maintenance guarantee

As said in the previous blog post, I am committed to maintaining at least 2 major releases simultaneously, unless something really unexpected happens and I have to leave the project. I understand how important stability is of a project that sits in-between multiple parts of a codebase, thus I am taking this seriously. Fixing bugs, and backporting features where possible, but naturally the latter is not always possible. I was focusing on 0.2 so far, but am going to finally port some of the changes back to 0.1, such as return slice wrapping!

Summary

All in all, CGlue is shaping up really nicely. Long future stands ahead, and I am very excited for it. I hope the project comes in handy for you, and you are able to try it out here.