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

Suggestion: generate permissive, C-compatible enums #2021

Open
BatmanAoD opened this issue Mar 8, 2024 · 0 comments
Open

Suggestion: generate permissive, C-compatible enums #2021

BatmanAoD opened this issue Mar 8, 2024 · 0 comments

Comments

@BatmanAoD
Copy link

It's often useful to interoperate with C libraries that may return enum values; in this case you do not necessarily have a guarantee that the returned value is a valid variant, and it may be useful to preserve the original value, e.g. for round-tripping data.

It's simple enough to represent this in Rust:

#[repr(u32)]
enum Foo {
    KnownValue1 = 1,
    KnownValue2 = 2,
    Unknown(u32),
}

...and then explicitly implement From<u32> for Foo and From<Foo> for u32. Here's a library that provides a macro for defining enums in this way: https://github.com/cimbul/tartan-c-enum

This seems like something that would be useful for UniFFI to support as well.

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

No branches or pull requests

1 participant