rust-lang / rust-lang/rust-bindgen
Cannot override integer type of constified enum module.
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
Hi,
Sorry if I'm missing something but there doesn't seem to be a way to override the integer type of a constified enum module.
I tried the naive approach of using ParseCallbacks::int_macro, but to no avail.
Input C Header
typedef uint16_t node_kind;
#define NODE_KIND_LIST_SHIFT 6
enum node_kinds {
NODE_KIND_FOO = 1,
NODE_KIND_BAR,
// ...
NODE_KIND_FOO_LIST = 1 << NODE_KIND_LIST_SHIFT,
NODE_KIND_BAR_LIST,
};
Bindgen Invocation
use bindgen::{callbacks::{IntKind, ParseCallbacks}, Builder, EnumVariation};
struct MyCallbacks;
impl ParseCallbacks for MyCallBacks {
// This works for macros, but not for enum members
fn int_macro(&self, name: &str, value: i64) -> Option<IntKind> {
match name {
s if s.starts_with("NODE_KIND_") => {
assert!(value >= 0 && value <= u16::MAX as _, "{name} ({value}) overflows u16 bounds.");
Some(IntKind::U16)
}
_ => None,
}
}
}
Builder::default()
.header("input.h")
.default_enum_style(EnumVariation::ModuleConsts)
.parse_callbacks(Box::new(MyCallbacks))
.generate()
.unwrap()
Actual Results
pub type node_kind = u16;
pub const NODE_KIND_LIST_SHIFT: u16 = 6;
pub mod node_kinds {
pub type Type = ::std::os::raw::c_uint;
pub const NODE_KIND_FOO: Type = 1;
pub const NODE_KIND_BAR: Type = 2;
// ...
pub const NODE_KIND_FOO_LIST: Type = 64;
pub const NODE_KIND_BAR_LIST: Type = 65;
}
Expected Results
There should be a way to obtain the following result:
pub type node_kind = u16;
pub const NODE_KIND_LIST_SHIFT: u16 = 6;
pub mod node_kinds {
pub type Type = u16;
pub const NODE_KIND_FOO: Type = 1;
pub const NODE_KIND_BAR: Type = 2;
// ...
pub const NODE_KIND_FOO_LIST: Type = 64;
pub const NODE_KIND_BAR_LIST: Type = 65;
}
As a side note, this is another use case for #1916.
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 tracing ParseCallbacks::int_macro and EnumVariation::ModuleConsts to determine how the integer type for constified enum members is selected. Verify the generated module against the issue's expected u16 output, while preserving the existing macro behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100