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 user defined enums #5

Open
Joey9801 opened this issue Aug 29, 2019 · 0 comments
Open

Support user defined enums #5

Joey9801 opened this issue Aug 29, 2019 · 0 comments
Labels
Types This issue is about support for specific types/classes of types

Comments

@Joey9801
Copy link
Owner

Rust enum types are tagged unions. They don't have a fixed representation, eg the compiler optimises Option<&T> to elide the tag entirely, and reduces it to a single pointer where a NULL pointer represents the None variant.

The general case for generating bindings for arbitrary Rust enums is relatively involved. Several new structs need to be emitted + have binding metadata generated for them:

  • A non-anonymous struct for each enum variant that holds data
  • A C style union of each of those non-anonymous structs
  • One final struct which contains an appropriately sized discriminator + an instance of the C style union.

Example:

#[dotnet_bindgen]
enum ExampleEnum {
    Foo,
    Bar(i32, i32),
    Baz { field_1: i32, field_2: i32 }
}

generates the following structs

#[repr(C)]
#[dotnet_bindgen]
pub struct __data_Bar (i32, i32)

#[repr(C)]
#[dotnet_bindgen]
pub struct __data_Baz {
    field_1: i32,
    field_2: i32,
}

#[repr(C)]
pub union __data {
    Bar: (i32, i32),
    Baz: __data_Baz,
}

struct __bindgen_enum_ExampleEnum {
    discriminator: u8,
    data: __data,
}

Normal binding metadata should be generated for each of the new explicitly named data variant structs, which can be easily accomplished by attaching the #[dotnet_bindgen] attribute to them.
The metadata emitted for the enumeration should include a mapping from discriminator value to variant.

TODO: The C# that should be generated for the above example.

@Joey9801 Joey9801 added the Types This issue is about support for specific types/classes of types label Aug 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Types This issue is about support for specific types/classes of types
Projects
None yet
Development

No branches or pull requests

1 participant