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

Better handling of optional properties in Rust #45

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

pchampin
Copy link

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 as i8 or u8).

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 in Option<...>.

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 in Option<...>). 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 with metadata.rustType.

This PR is still marked WIP because

  • maybe it can be made a little less defensive (see above), and
  • the test suite needs to be adapted to the proposed change.

But before continuing, I would like to get the maintainers opinion on that proposition ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant