Generate Rust enums from bindgen output#32
Conversation
|
Sorry, I completely missed that this was submitted. It'd be certainly practical to have better ergonomics than doing this manually in riot-wrappers. I'm not sure how this is best usable: There is no This looks very similar to what the What riot-wrappers sometimes does is to add an |
|
No problem it's not very important :) For the The rustified enum output would rely on RIOT never returning "invalid"-enum-values if I read the info from the link you provided correctly? So to me it seems safer to keep the int return/enum types, generate the Rust-variants and let the wrapper around the foreign-function decide how to proceed e.g. by calling let c_enum: i32 = riot_sys::some_c_function();
return RustEnum::try_from(c_enum);This should basically be what riot-wrappers is doing with the fallible conversion enums just automated. |
C enums handled by bindgen are translated into something like this:
pub const c_enum_name_enum_variant. This is a bit annoying when someone wants to use them as a correct Rust enum, because one has to basically rewrite and maintain a separate Rust enum. The idea here is now to provide a list inside ofbuild.rsjust like with the macros where enums can be registered to be translated into Rust.build.rswill scan the bindgenoutput for the c enums and create the corresponding Rust enums inenums.rsThis would have the side effect, that the enum list would automatically updated with RIOT. I am not 100% sure this is desirable but if one would mark the enums as
non_exhaustive"older" Rust code should still work with new RIOT versions. My hope is that this could make things a bit easier to maintain and build.