Best practice when working with `rust-bindgen` and `libc`?

Hi folks!

I recently generated the bindings to the ALSA library on my NixOS system in order to update CPAL's bindings. I believe the original set were partly generated and partly hand-rolled, but they've been around since 2014 and are missing functionality I need so I thought I would give them an update.

One difference between the original bindings and the new generated set is that the original set use libc, while the latest rust-bindgen seems to have generated everything needed from the system libraries. That is, the generated bindings include definitions for items like pollfd, poll, POLLIN, POLLOUT, etc that are normally found in libc.

This is nice in the sense that it seems to remove the need for the libc dependency, but I'm curious if there is a benefit to using libc that I'm overlooking. E.g. is it still better practise to use libc where possible to reduce the need for every set of system bindings to have their own definitions of these types? Or is this extra code-gen negligible in practice? What are the best practices in this case?

In the case that you would recommend using libc, what is the best way to go about this? There does not seem to be an option in rust-bindgen to use libc where possible, only options for specifying blacklists/whitelists manually which seems like a very tedious approach.

Any thoughts/advice appreciated!

1 Like

I guess it is hard to prevent bindgen from redefining some libc constants that the lib reexports, and I don't think that it is a problem (if it were, you can always offer the option to regenerate the bindings to load from the latest alsa headers).

Regarding ::libc::<c_type> vs. ::std::os::raw::<c_type>, there is a test in the standard library that asserts they are equivalent, so I guess that not explicitely pulling libc is an aesthetic improvement (it is still a dependency of the standard library, so there is no difference in practice).

You can nevertheless override that last change using the --ctypes-prefix "::libc::" bindgen flag.

1 Like