Skip to content

Commit f832add

Browse files
committed
Make std::mem::transmute_copy accept ?Sized inputs
This enables conversions from dynamically-sized types like `[u8]`.
1 parent 51f714c commit f832add

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

library/core/src/mem/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1052,14 +1052,14 @@ pub const fn copy<T: Copy>(x: &T) -> T {
10521052
#[must_use]
10531053
#[stable(feature = "rust1", since = "1.0.0")]
10541054
#[rustc_const_unstable(feature = "const_transmute_copy", issue = "83165")]
1055-
pub const unsafe fn transmute_copy<Src, Dst>(src: &Src) -> Dst {
1055+
pub const unsafe fn transmute_copy<Src: ?Sized, Dst>(src: &Src) -> Dst {
10561056
assert!(
1057-
size_of::<Src>() >= size_of::<Dst>(),
1057+
size_of_val(src) >= size_of::<Dst>(),
10581058
"cannot transmute_copy if Dst is larger than Src"
10591059
);
10601060

10611061
// If Dst has a higher alignment requirement, src might not be suitably aligned.
1062-
if align_of::<Dst>() > align_of::<Src>() {
1062+
if align_of::<Dst>() > align_of_val(src) {
10631063
// SAFETY: `src` is a reference which is guaranteed to be valid for reads.
10641064
// The caller must guarantee that the actual transmutation is safe.
10651065
unsafe { ptr::read_unaligned(src as *const Src as *const Dst) }

0 commit comments

Comments
 (0)