getditto / safer_ffi

Write safer FFI code in Rust without polluting it with unsafe code
http://getditto.github.io/safer_ffi
MIT License
926 stars 40 forks source link

Enum with fields #63

Open DBLouis opened 3 years ago

DBLouis commented 3 years ago

Is it possible to add support for #[repr(C, Int)] enums?

In the Type layout reference it says:

The representation of a repr(C) enum with fields is a repr(C) struct with two fields, also called a "tagged union" in C

I am not sure is this is guaranteed tho. If it is we should be able to transmute both ways right?

Playground test: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fe82b419608d325d93481155c4458bc8

danielhenrymantilla commented 3 years ago

Indeed, that ought to be the implementation 👌 I am waiting on this since I'd like to bundle support for repr_c::Result from there, and I want to think a bit about the best design / layout (e.g., C ergonomics) for such a thing 🙂

DBLouis commented 3 years ago

I had a go at it but I'm stuck making the macro match all kinds of enums variants.

danielhenrymantilla commented 3 years ago

Yeah, I think this will require https://github.com/getditto/safer_ffi/issues/46, the current situation with macro_rules! macros to save a bit of compile time is not worth the maintainability cost