-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Make std::mem::transmute_copy
accept ?Sized
inputs
#112457
base: master
Are you sure you want to change the base?
Conversation
r? @scottmcm (rustbot has picked a reviewer for you, use r? to override) |
@rustbot label +T-libs-api -T-libs |
622eacf
to
f5bc1e6
Compare
This enables conversions from dynamically-sized types like `[u8]`.
f5bc1e6
to
c9575a6
Compare
library/core/src/mem/mod.rs
Outdated
assert!( | ||
size_of::<Src>() >= size_of::<Dst>(), | ||
size_of_val(src) >= size_of::<Dst>(), | ||
"cannot transmute_copy if Dst is larger than Src" | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer guaranteed to be compiled out in --release
. I do not believe we want this assert in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed this to debug_assert!
in 3f1825a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this not guaranteed to be compiled out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tbu- because size_of_val
for dynamically-sized types requires a runtime check since the type's size is not known at compile-time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing this to debug_assert!
means this check no longer triggers for 99% of users who do not work with a debug build of the stdlib.
Code that uses this function is likely performance-sensitive, so we do not want to emit an assert for dynamically-sized types in release mode.
Transmute only requires the bits to be valid for Do we have any requirements on custom DSTs that their size must be computable at all times? One might get funny effects if such a type is trying to compute its size in the middle of unsafe code mucking with its bytes. |
@the8472 |
Hrrm, right, only the source is unsized. Still, the assert would try to compute the dynamic size of the source in debug builds of the standard library. |
Flipping this over to a libs-api reviewer since this needs an FCP due to being new stable capabilty |
Tangentially: |
Out of curiosity, do you have any uses in mind for anything but But coming from |
☔ The latest upstream changes (presumably #115520) made this pull request unmergeable. Please resolve the merge conflicts. |
@nvzqz would be nice to have a reply to #112457 (comment) and can resolve the conflicts if interested in moving this forward? thanks |
This enables conversions from dynamically-sized types like
[u8]
.