Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mapping Enums to Result type #3102

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jschwe
Copy link
Contributor

@jschwe jschwe commented Jan 29, 2025

Some enums, where the Ok variant is 0, can be mapped to a Rust Result<(), NonZero<NewType>> representation, where the enum newtype is generated without the Ok variant.
This PR adds a new option for the user to select this new Result representation for the enum, which can make the generated APIs more ergonomic, when C functions use the result enum as their return type.

Additionally, a parse callback is added, which allows renaming the new error enum (which does not contain the Ok / 0 variant).

As discussed in the linked issue, since Rust currently doesn't allow arbitrary niches, this new enum mapping can only work with C result enums, where 0 is the Ok value. Other error enums are out of scope.

Closes #2980

@jschwe jschwe force-pushed the jschwender/result-enums branch from 44a7a8e to 5a10e24 Compare January 30, 2025 09:17
Some enums, where the Ok variant is 0, can be mapped
to a Rust Result, with a NonZero<NewType> enum representation.

Future improvements could add a callback which allows
customizing the name error enum.
@jschwe jschwe force-pushed the jschwender/result-enums branch from 5a10e24 to 2a2605e Compare January 30, 2025 09:33
@jschwe jschwe force-pushed the jschwender/result-enums branch from 2a2605e to 1464db3 Compare January 30, 2025 09:43
There is a `no_run`, but not `no_build` doctest attribute, so we need to ignore it
to prevent CI from failing in the msrv build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Result<(), error_enum> enum binding
1 participant