Rollup merge of #71446 - Amanieu:transmute_copy, r=sfackler
Only use read_unaligned in transmute_copy if necessary I've noticed that this causes LLVM to generate poor code on targets that don't support unaligned memory accesses.
This commit is contained in:
commit
199f4deef0
@ -924,7 +924,12 @@ pub fn drop<T>(_x: T) {}
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
|
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.
|
/// Opaque type representing the discriminant of an enum.
|
||||||
|
Loading…
Reference in New Issue
Block a user