diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index 39c9a04eea92b..87b10d17aab3f 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -1052,14 +1052,14 @@ pub const fn copy(x: &T) -> T { #[must_use] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_transmute_copy", issue = "83165")] -pub const unsafe fn transmute_copy(src: &Src) -> Dst { - assert!( - size_of::() >= size_of::(), +pub const unsafe fn transmute_copy(src: &Src) -> Dst { + debug_assert!( + size_of_val(src) >= size_of::(), "cannot transmute_copy if Dst is larger than Src" ); // If Dst has a higher alignment requirement, src might not be suitably aligned. - if align_of::() > align_of::() { + if align_of::() > align_of_val(src) { // SAFETY: `src` is a reference which is guaranteed to be valid for reads. // The caller must guarantee that the actual transmutation is safe. unsafe { ptr::read_unaligned(src as *const Src as *const Dst) } diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs index 5c2e18745ea21..0b73a4d56d34e 100644 --- a/library/core/tests/mem.rs +++ b/library/core/tests/mem.rs @@ -121,6 +121,11 @@ fn test_transmute_copy_shrink() { assert_eq!(0_u8, unsafe { transmute_copy(&0_u64) }); } +#[test] +fn test_transmute_copy_slice() { + assert_eq!(1, unsafe { transmute_copy(&[1][..]) }); +} + #[test] fn test_transmute_copy_unaligned() { #[repr(C)]