Create a type alias for bindgen

This is a follow-up to this question: Use existing bindings from a *-sys crate with bindgen

I have a *-sys crate that depends on the system library glib. I found that bindings to items from glib already exist in the glib-sys crate. Currently, my *-sys crate re-generates those bindings. However, I want to use the ones that already exist under glib-sys.

To do this, I blocklisted glib's types in my build.rs:

let bindings = bindgen::Builder::default()
    // ...
    .blocklist_type(r"(?i)_?G\w+")

However, if bindgen does not generate these, the crate fails to build as it cannot find those types. I want to instruct bindgen that they do in fact exist under glib-sys. I was advised to do this using type aliases, but I can't find any way to do so.

Did adding a type alias in the crate (in another file) next to the generated bindings not work?

1 Like

How are you using the resulting generated file? If they are include!d somewhere, then any type at the same level as the include will be seen inside the included code.

1 Like

I'm now trying to generate the bindings this way:

let bindings = bindgen::Builder::default()
    // ...
    .allowlist_var("Playerctl_.*")
    .allowlist_type("Playerctl_.*")
    .allowlist_function("playerctl_.*")
    .blocklist_type(r"(?i)\b_?G\w+")
    // ...

I also created a new file types.rs which I include alongside bindings.rs. However, one of the types that I have to define in types.rs requires __BindgenBitfieldUnit, which isn't provided by glib_sys. I get the following error when trying to build:

error[E0412]: cannot find type `__BindgenBitfieldUnit` in this scope
  --> src/types.rs:28:22
   |
28 |     pub _bitfield_1: __BindgenBitfieldUnit<[u8; 4]>,
   |                      ^^^^^^^^^^^^^^^^^^^^^ not found in this scope

If I remove the call to blocklist_type, this struct still generates. I'm confused about this struct-- it doesn't match the pattern (?i)\b_?G\w+, so I don't know why it isn't generating. I tried

  • adding a line .allowlist_type("__BindgenBitfieldUnit") and
  • modifying .allowlist_type("Playerctl_.*") to .allowlist_type("Playerctl_.*|__BindgenBitfieldUnit"),

but neither of these result in the struct being generated.

To find what I was missing, I removed the calls to allowlist_*, blocklist_type, and the include!("types.rs"). This generated the full bindings.rs along with all of the types from glib-sys. I copied the structs I was missing from that file and placed them in my types.rs. Now, the crate has everything it needs 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.