uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

Suggestion: generate permissive, C-compatible enums

Open BatmanAoD opened this issue 1 year ago • 11 comments

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.

BatmanAoD avatar Mar 08 '24 17:03 BatmanAoD