rust-lang / rust-lang/rust-bindgen
Question: Is it possible to set a name for anonymous unions using an annotation comment to the C/C++ source code?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
I would like to know if is possible to suggest to bindgen a name for anonymous unions.
For example, if I have this structure:
typedef struct
{
unsigned int tCL;
union {
unsigned int tRCD;
unsigned int tRCD_RD;
};
unsigned int tRCD_WR,
tRP,
tRAS,
tRC,
tRCPB,
tRPPB;
union {
unsigned int tRRD;
unsigned int tRRDS;
};
unsigned int tRRDL,
tRRDDLR,
tFAW,
tFAWSLR,
tFAWDLR,
tWTRS,
tWTRL,
tWR;
} RAM_TIMING;
bindgen generates a rust struct like this:
#[repr(C)]
#[derive(Copy, Clone)]
pub struct RAM_TIMING {
pub tCL: ::std::os::raw::c_uint,
pub __bindgen_anon_1: RAM_TIMING__bindgen_ty_1,
pub tRCD_WR: ::std::os::raw::c_uint,
pub tRP: ::std::os::raw::c_uint,
pub tRAS: ::std::os::raw::c_uint,
pub tRC: ::std::os::raw::c_uint,
pub tRCPB: ::std::os::raw::c_uint,
pub tRPPB: ::std::os::raw::c_uint,
pub __bindgen_anon_2: RAM_TIMING__bindgen_ty_2,
pub tRRDL: ::std::os::raw::c_uint,
pub tRRDDLR: ::std::os::raw::c_uint,
pub tFAW: ::std::os::raw::c_uint,
pub tFAWSLR: ::std::os::raw::c_uint,
pub tFAWDLR: ::std::os::raw::c_uint,
pub tWTRS: ::std::os::raw::c_uint,
pub tWTRL: ::std::os::raw::c_uint,
pub tWR: ::std::os::raw::c_uint
}
I was wondering if there is a way to specify a name for all the anonymous fiels, like __bindgen_anon_1 and RAM_TIMING__bindgen_ty_1.
I don't know if it makes sense, but I was hoping to be able to do something like:
typedef struct
{
unsigned int tCL;
/// <div rustbindgen name="tRCD_u"></div>
union {
unsigned int tRCD;
unsigned int tRCD_RD;
};
[...]
to generate something like this:
#[repr(C)]
#[derive(Copy, Clone)]
pub struct RAM_TIMING {
pub tCL: ::std::os::raw::c_uint,
pub tRCD_u: RAM_TIMING__tRCD_u,
[...]
Unfortunately I cannot simply give a name to the union. If the comment solution is not possible, is there any other way to control the name used by the generated code in case of anonymous unions?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing bindgen's handling of anonymous unions and whether source comments are inspected for naming hints. Compare the requested C/C++ example with the generated Rust names, then determine whether the result should be implemented or documented as unsupported. Done means establishing a clear way to control both generated anonymous-union names, or clearly explaining why it is not possible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, cpp, rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100