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

Use a stable repr to enable safe casting #359

Closed
jean-airoldie opened this issue Apr 27, 2021 · 3 comments · Fixed by #364
Closed

Use a stable repr to enable safe casting #359

jean-airoldie opened this issue Apr 27, 2021 · 3 comments · Fixed by #364
Milestone

Comments

@jean-airoldie
Copy link
Contributor

I've been looking in storing Decimal inline in c struct so that I don't have to pay serialization & deserialization cost. However I've noticed that the Decimal type does not have a stable ABI represenation.

Would you consider using #[repr(C)] or #[repr(u128]) in order to provide a stable ABI? This would basically deprecate the serialize & deserialize methods, but lock-in the current inner represation. I'm guessing this shouldn't impact performance.

For my use case, the main benefit would be to be able to safely cast a &[u8] into &[Decimal], which currently cannot be done safely without allocating an intermediary buffer etc.

@paupino
Copy link
Owner

paupino commented Apr 28, 2021

This is actually something I looked at a while ago and would be open to adding it again. Perhaps to begin with, placing it behind a feature flag could be useful for experimentation purposes.

@c410-f3r
Copy link
Contributor

Just for the record. rust-lang/rust#81996

@jean-airoldie
Copy link
Contributor Author

jean-airoldie commented Apr 30, 2021

From my understanding of the issue mentioned by @c410-f3r, repr(C) is unsounds for some zero-sized types and can allow overflowing explicit discriminants for enums. So as far as I understand it, it shouldn't affect the decimal's repr.

However, for some additional sanity, I think it would be better to add some layout assertion tests, to make sure the layout is really what we expect. In that case, simply making sure the size of the decimal type is 16 bytes would be enough, I believe.

jean-airoldie added a commit to jean-airoldie/rust-decimal that referenced this issue Apr 30, 2021
This adds the c-repr feature which forces `Decimal` to be #[repr(C)].

Closes paupino#359.
@paupino paupino added this to the v1.13 milestone Apr 30, 2021
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 a pull request may close this issue.

3 participants