rust-lang / rust-lang/rust-bindgen
Constified to Rustified Converter
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
Defaulting to using constified enums makes sense when dealing with C libraries, given that Rust has undefined behavior in the event of the C library expanding its enum.
However, when doing development on the rust end, it is nice to be able to have facilities like #[derive(Debug)] or exhaustive match.
I'd like to request an option to generate both a Rustified enum and a constified enum, use the constified representation in the library interface, and provide EnumName::from_constified(k: u32) -> Option<EnumName> or similar function to represent a possibly failing (if the const is out of range) translation to a Rustified enum.
I think this could get us the best of both worlds. It avoids undefined behavior, but still allows the use of a more Rust-like data structure with an explicit place to catch the error.
(I'd tag this Enhancement if I could, it's not a bug)
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 locating the enum-generation paths that handle Rustified and constified representations. Trace how generated enums are exposed through the binding interface, then define the option and conversion behavior requested here. Done means both representations can be generated, the interface remains constified, and conversion from a constified value can report an invalid value.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100