Skip to content

Commit

Permalink
Only use read_unaligned in transmute_copy if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed Apr 22, 2020
1 parent 82e90d6 commit 99de372
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/libcore/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 99de372

Please sign in to comment.