rust-lang / rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
https://rust-lang.github.io/rust-bindgen/
BSD 3-Clause "New" or "Revised" License
4.23k stars 679 forks source link

Consider using field names for anonymous types/unions #2759

Open HadrienG2 opened 4 months ago

HadrienG2 commented 4 months ago

While they have a good chance of avoiding namespace collisions, numbered anonymous types are decidedly not very readable.

image

It would be nice if bindgen could use the field name instead when naming the anonymous type. In the above example, hwloc_bridge_attr_s__bindgen_ty_1 would become hwloc_bridge_attr_s__upstream and hwloc_bridge_attr_s__bindgen_ty_2 would become hwloc_bridge_attr_s__downstream.

pvdrz commented 3 months ago

My first impression is that this is a good idea. My main thoughts on the subject are:

TLDR: Sounds like a desirable thing, just needs someone implementing so we can figure out small details that might come up.

HadrienG2 commented 3 months ago

There might be name collisions if two structs have fields with the same name. So that means that we cannot drop the number suffix at the end in all cases.

Sorry, I did not understand this part. Can this really happen if the type name contains the name of the struct as a prefix, as in my proposal? In my mind, if hwloc_bridge_attr_s::upstream is a unique entity in C (and it should be), then there is no reason why a matching autogenerated hwloc_bridge_attr_s__upstream Rust type name shouldn't be unique.

Okay, technically, a C library could have another type using the same naming convention (literally a struct hwloc_bridge_attr_s__upstream {};), but who does that? ;)