Add mappings in generated Rust code to eliminate some boilerplates and improve safety #28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What was supposed to just be a check of the enums to bypass the undefined behaviour raised in #23 ended up in a large refactoring of the Rust codegen with more powerful mappings to eliminate as much manual labour as possible.
Because some mappings can happen in structs, the solution was to hide the C fields and provide public accessors/mutators to interact with them in a Rusty fashion. To help creating new structs from scratch, a
MyStructFields
(notice the extra "Fields") is generated for eachMyStruct
. This struct has all its Rusty members public and implementsFrom<MyStructFields> for MyStruct
meaning you can do something like:The current mappings are:
c_int
toMyEnum
. The conversion makes sure that the enum value is valid and panics if it's not. This is better than the undefined behaviour of current Rust.*const MyStruct
toOption<&MyStruct>
.None
is mapped tostd::ptr::null()
.u64
/f64
tostd::time::Duration
.*const c_char
to&std::ffi::CStr
.Also, I removed the empty safety comments in the generated code, so we'll need to add a
#![allow(clippy::missing_safety_doc)]
at the beginning of ourffi.rs
files. For some reason, I cannot put this in the generated file.