References on creating Rust bindings?

I'm currently building Rust to C bindings for libpcap and WinPcap, and was wondering if there were references out there I could read up on to learn what idiomatic Rust language bindings look like.

So far, I'm aware of the unsafe chapter in TRPL, the blog post Manish wrote about SDL bindings, and the existence of the bindgen crate that could automate the whole process for me anyway, at least for the raw bindings.

Have you looked at any existing libpcap/wincap crates, like https://crates.io/crates/pcap? Or is this just a learning exercise?

Idiomatic way is to split bindings into two crates:

  1. low-level pure C-like interface, i.e. a -sys crate.
  2. higher-level Rust wrapper build on top of the sys crate.

I've written up a guide for the first part: Using C libraries in Rust: make a sys crate

4 Likes

@vitalyd I have, the problem is that the code needs to be restructured into smaller files, and I felt that the time I spent getting up to speed understanding how best to do that could be spent on getting my own workflow going. The aim of this is to further my understanding of the problem domain.

@kornel Funny you mention this approach, someone I met today working on Rust bindings told me the exact same thing :). Thanks heaps for your guide, I'll take a look at it!