rust-bindgen
rust-bindgen copied to clipboard
Consider using field names for anonymous types/unions
While they have a good chance of avoiding namespace collisions, numbered anonymous types are decidedly not very readable.
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
.
My first impression is that this is a good idea. My main thoughts on the subject are:
- 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.
- This is a breaking change. It could be a welcome one as it could reduce the breaking changes in libraries using bindgen (if we manage to drop the number at the end of the type unless is required).
- The internal changes required to achieve this might be larger than expected if we don't have the name of the field while deciding the name of the type.
TLDR: Sounds like a desirable thing, just needs someone implementing so we can figure out small details that might come up.
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? ;)