Adventures with Rust's FFI.

Adventures with Rust's FFI.

This past week I've been working on generating Rust bindings to the GnuTLS library. At first, I tried doing things manually, but ultimately gave up after a few hours. I didn't want to do any work, I just wanted quick bindings dammit.

I then stumbled across rust-bindgen, a project that automagically generates Rust FFI code given an input file. Too good to be true? Almost.

The first thing I discovered after generating FFI code was: there's a bug with representing univariant enums in Rust. This is a well documented bug and the issue on GitHub can be found here.

Example:

    enum Foo {
        BAR
    }

This can be remedied by adding a dummy enum item.

The second thing is type aliasing between enums is not as straight forward as I thought it was. For example, the following would not work:

    enum Foo {
       HELLO,
       WORLD
    }

    type Bar = Foo;
    assert_eq!(Foo::HELLO, Bar::WORLD)

The fix to this problem involved a whole lot of massaging generated output to get rid of the aliases. Fun.

Regardless, rust-bindgen saved me hours of work and I learned a few things along the way.