Only use read_unaligned in transmute_copy if necessary

This commit is contained in:
Amanieu d'Antras 2020-04-22 22:22:48 +01:00
parent 82e90d6426
commit 99de3728f9

View File

@ -923,7 +923,12 @@ pub fn drop<T>(_x: T) {}
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
ptr::read_unaligned(src as *const T as *const U)
// If U has a higher alignment requirement, src may not be suitably aligned.
if align_of::<U>() > align_of::<T>() {
ptr::read_unaligned(src as *const T as *const U)
} else {
ptr::read(src as *const T as *const U)
}
}
/// Opaque type representing the discriminant of an enum.