Skip to content

Add Result<(), error_enum> enum binding #2980

Open
@jschwe

Description

@jschwe

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions