rust-lang / rust-lang/rust-bindgen

Add Result<(), error_enum> enum binding

Open
#2,980 10 comments 0 reactions 1 assignee View on GitHub

@pvdrz is already working on this.

Since Dec 10, 2024.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

For enums representing errors, often the Ok variant is the 0 value of the enum. It would be convenient if bindgen could replace this enum with a Result on the Rust side.

Example C-enum:

enum MY_C_ERRORCODE {
   MY_C_ERRORCODE_SUCCESS,
   MY_C_ERRORCODE_ERROR1,
   MY_C_ERRORCODE_ERROR2,
}

Expected Rust binding:

#[repr(transparent)]
pub struct MY_C_ERRORCODE_NZ(NonZero<c_int>);

impl MY_C_ERRORCODE {
    pub const ERROR1 = MY_C_ERRORCODE_NZ(NonZero::new(1).unwrap()));
    pub const ERROR2 = MY_C_ERRORCODE_NZ(NonZero::new(2).unwrap()))
}

// Perhaps this typedef could be omitted, and usages in the API could just use the result directly
type MY_C_ERRORCODE = Result<(), MY_C_ERRORCODE_NZ>;

This should work well at least when assuming that the generated non-zero enum representation is Newtype based.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.