rust-lang / rust-lang/rust-bindgen
Feature Request: Allow constraining multiple macro-defines to have the same type
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
Problem
Some C libraries choose to use macro-defines where enums are also applicable (likely due to the strange behaviour of enums in C). While this is fine in principle, when creating an ffi to Rust using bindgen, this can result in a set of associated constants having wildly different types.
Consider this example from lm-sensors' libsensors:
#define SENSORS_BUS_TYPE_ANY (-1)
#define SENSORS_BUS_TYPE_I2C 0
#define SENSORS_BUS_TYPE_ISA 1
#define SENSORS_BUS_TYPE_PCI 2
#define SENSORS_BUS_TYPE_SPI 3
#define SENSORS_BUS_TYPE_VIRTUAL 4
#define SENSORS_BUS_TYPE_ACPI 5
#define SENSORS_BUS_TYPE_HID 6
#define SENSORS_BUS_TYPE_MDIO 7
#define SENSORS_BUS_TYPE_SCSI 8
passing this through bindgen (here with fit_macro_constants(false)) results in the following set of constants:
pub const SENSORS_BUS_TYPE_ANY: i32 = -1;
pub const SENSORS_BUS_TYPE_I2C: u32 = 0;
pub const SENSORS_BUS_TYPE_ISA: u32 = 1;
pub const SENSORS_BUS_TYPE_PCI: u32 = 2;
pub const SENSORS_BUS_TYPE_SPI: u32 = 3;
pub const SENSORS_BUS_TYPE_VIRTUAL: u32 = 4;
pub const SENSORS_BUS_TYPE_ACPI: u32 = 5;
pub const SENSORS_BUS_TYPE_HID: u32 = 6;
pub const SENSORS_BUS_TYPE_MDIO: u32 = 7;
pub const SENSORS_BUS_TYPE_SCSI: u32 = 8;
Note that the ideal type for these constants would actually be c_short as that is how the macros are used in libsensors itself.
In this case, using these constants in a match-statement is impossible, due to SENSORS_BUS_TYPE_ANY receiving a different signedness than the other constants.
If fit_macro_constants were true, it is even conceivable that some values may become u16 while others are u8. As you can see, the current lack of configuration options causes all sorts of problems.
Feature Request
As such, I suggest that there should at minimum be some sort of group_macro_constants_types method on the Builder that accepts a regex pattern and causes the builder to force all constants generated from macros matching the pattern to have the same type, either inferred from the constants or explicitly specified as another parameter.
It may also be useful to allow macro constants matching a pattern to be treated as though they were c-enums or at least allow them to be grouped into a module similar to constified_enum_module's behaviour.
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 the Builder configuration around fit_macro_constants and the constified_enum_module behavior mentioned in the issue. Determine how a regex-selected group of macro constants would receive a shared inferred or explicit type; done means the requested grouping and type constraint are supported and covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100