@@ -100,16 +100,19 @@ pub use unique::Unique;
100100/// as the compiler doesn't need to prove that it's sound to elide the
101101/// copy.
102102///
103+ /// Unaligned values cannot be dropped in place, they must be copied to an aligned
104+ /// location first using [`ptr::read_unaligned`].
105+ ///
103106/// [`ptr::read`]: ../ptr/fn.read.html
107+ /// [`ptr::read_unaligned`]: ../ptr/fn.read_unaligned.html
104108///
105109/// # Safety
106110///
107111/// Behavior is undefined if any of the following conditions are violated:
108112///
109113/// * `to_drop` must be [valid] for reads.
110114///
111- /// * `to_drop` must be properly aligned. See the example below for how to drop
112- /// an unaligned pointer.
115+ /// * `to_drop` must be properly aligned.
113116///
114117/// Additionally, if `T` is not [`Copy`], using the pointed-to value after
115118/// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
@@ -153,31 +156,6 @@ pub use unique::Unique;
153156/// assert!(weak.upgrade().is_none());
154157/// ```
155158///
156- /// Unaligned values cannot be dropped in place, they must be copied to an aligned
157- /// location first:
158- /// ```
159- /// use std::ptr;
160- /// use std::mem::{self, MaybeUninit};
161- ///
162- /// unsafe fn drop_after_copy<T>(to_drop: *mut T) {
163- /// let mut copy: MaybeUninit<T> = MaybeUninit::uninit();
164- /// ptr::copy(to_drop, copy.as_mut_ptr(), 1);
165- /// drop(copy.assume_init());
166- /// }
167- ///
168- /// #[repr(packed, C)]
169- /// struct Packed {
170- /// _padding: u8,
171- /// unaligned: Vec<i32>,
172- /// }
173- ///
174- /// let mut p = Packed { _padding: 0, unaligned: vec![42] };
175- /// unsafe {
176- /// drop_after_copy(&mut p.unaligned as *mut _);
177- /// mem::forget(p);
178- /// }
179- /// ```
180- ///
181159/// Notice that the compiler performs this copy automatically when dropping packed structs,
182160/// i.e., you do not usually have to worry about such issues unless you call `drop_in_place`
183161/// manually.
0 commit comments