Better handling of optional properties in Rust #45
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.
The problem
Currently, optional properties have their type systematically wrapped in
Option<Box<...>>
in Rust. This is not satisfying. This creates an unnecessary allocation on the heap, as well as a significant memory overhead for atomic types (especially small ones such asi8
oru8
).The
Box
is actually here only to ensure that the generated Rust code will compile when recursive types are involved.Proposed solution
This PR introduces a detection of recursive types, and only uses
Box
for them. Any other type in optional properties is now simply wrapped inOption<...>
.Note that this strategy it is still overly defensive. Recursive types are wrapped in
Box
everywhere, including places where this would not be necessary (and not just inOption<...>
). This could maybe be improved, but I consider that this is still better than the current situation. Of course the user can still override this withmetadata.rustType
.This PR is still marked WIP because
But before continuing, I would like to get the maintainers opinion on that proposition ;)