Make the Pod
/NoUninit
derive macros not use transmute
, to be more robust against possible future relaxations transmute
's size check.
#320
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.
RFC 3844 is proposing relaxations to
std::mem::transmute
's semantics, and under some possible relaxations,bytemuck_derive
's current usage of it to ensure soundness becomes insufficient1.This PR makes the
Pod
/NoUninit
derive not usetransmute
, and instead emit aconst _: () = assert!
that ensures there is no padding in the type by asserting that the type's size is the same as the sum of the type's fields' sizes.Concrete change: For the following source code:
The
Pod
derive macros' no-padding check is changed from:To this:
(
derive(NoUninit)
is the same, but the assert message mentionsNoUninit
instead ofPod
)Footnotes
Currently, the derive macros only mention
std::mem::transmute::<Foo, FooWithoutPadding>
, they don't call it, and under at least the proposed "just have aconst
assert that the sizes are the same in the body oftransmute
" change, this would no longer be enough to ensure a compiler error ifType1
andType2
have different sizes: playground link ↩