Make std::mem::transmute_copy accept ?Sized inputs#112457
Make std::mem::transmute_copy accept ?Sized inputs#112457nvzqz wants to merge 3 commits intorust-lang:masterfrom
std::mem::transmute_copy accept ?Sized inputs#112457Conversation
|
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.
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.
Changed this to debug_assert! in 3f1825a
There was a problem hiding this comment.
Why is this not guaranteed to be compiled out?
There was a problem hiding this comment.
@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.
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 |
|
Closing this as inactive. Feel free to reöpen this pr or create a new pr if you get the time to work on this. Thanks |
This enables conversions from dynamically-sized types like
[u8].