Skip to content

Commit 99de372

Browse files
committed
Only use read_unaligned in transmute_copy if necessary
1 parent 82e90d6 commit 99de372

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/libcore/mem/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,12 @@ pub fn drop<T>(_x: T) {}
923923
#[inline]
924924
#[stable(feature = "rust1", since = "1.0.0")]
925925
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
926-
ptr::read_unaligned(src as *const T as *const U)
926+
// If U has a higher alignment requirement, src may not be suitably aligned.
927+
if align_of::<U>() > align_of::<T>() {
928+
ptr::read_unaligned(src as *const T as *const U)
929+
} else {
930+
ptr::read(src as *const T as *const U)
931+
}
927932
}
928933

929934
/// Opaque type representing the discriminant of an enum.

0 commit comments

Comments
 (0)