rust-lang / rust-lang/rust-bindgen

Constified to Rustified Converter

Open
#1,459 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.