Use existing bindings from a *-sys crate with bindgen

I'm using bindgen to generate bindings for playerctl. It uses types and functions from glib, which bindgen is able to generate by itself. However, bindings to glib already exist in the glib-sys crate.

I know for certain that _GList and glib_sys::GList are identical. Here is the struct from that bindgen generated from glib, located in glib_sys:

#[repr(C)]
pub struct GList {
    pub data: gpointer,
    pub next: *mut GList,
    pub prev: *mut GList,
}

Here is the struct that bindgen generated from playerctl, located in playerctl_sys:

#[repr(C)]
pub struct _GList {
    pub data: gpointer,
    pub next: *mut GList,
    pub prev: *mut GList,
}

In a few of my tests, I attempted to replace a few bindgen-generated functions and structs with identical ones from glib-sys. They appear to not be compatible:

expected raw pointer `*mut glib_sys::GList`
   found raw pointer `*mut _GList`

How can I avoid this? I need to somehow instruct bindgen to use glib_sys instead of re-generating everything.

I believe you want to Blocklist the glib types/files and then create type aliases with the names bindgen expects that point to glib_sys's versions

1 Like

I attempted to remove the glib types this way:

let bindings = bindgen::Builder::default()
    // ...
    .allowlist_var("Playerctl_.*")
    .allowlist_type("Playerctl_.*")
    .allowlist_function("playerctl_.*")

This prevents most, but not all glib items from being generated. Some essential structs that appear in playerctl's function signatures, like GList from earlier, are still generated.

Is there a way to prevent this too?

Did you try blocklisting the glib types that still ended up being generated?

Yeah, I tried appending these

.blocklist_type("G_*")
.blocklist_type("_G_*")
.blocklist_type("g_*")

but it didn't change what was generated.

I don't think _GList matches any of those regular expressions

I replaced .blocklist_type("_G_*") with .blocklist_type(r"_G\w+"). Now, the crate fails to build with this error:

error[E0412]: cannot find type `_GList` in this scope
  --> /path/playerctl-sys/target/debug/build/playerctl-sys-e80adf4f6de29338/out/bindings.rs:24:18
   |
24 | pub type GList = _GList;
   |                  ^^^^^^ not found in this scope

Yeah you'll have to create a type alias referring to the glib_sys version for it to build

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.