@@ -100,16 +100,19 @@ pub use unique::Unique;
100
100
/// as the compiler doesn't need to prove that it's sound to elide the
101
101
/// copy.
102
102
///
103
+ /// Unaligned values cannot be dropped in place, they must be copied to an aligned
104
+ /// location first using [`ptr::read_unaligned`].
105
+ ///
103
106
/// [`ptr::read`]: ../ptr/fn.read.html
107
+ /// [`ptr::read_unaligned`]: ../ptr/fn.read_unaligned.html
104
108
///
105
109
/// # Safety
106
110
///
107
111
/// Behavior is undefined if any of the following conditions are violated:
108
112
///
109
113
/// * `to_drop` must be [valid] for reads.
110
114
///
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.
113
116
///
114
117
/// Additionally, if `T` is not [`Copy`], using the pointed-to value after
115
118
/// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
@@ -153,31 +156,6 @@ pub use unique::Unique;
153
156
/// assert!(weak.upgrade().is_none());
154
157
/// ```
155
158
///
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
- ///
181
159
/// Notice that the compiler performs this copy automatically when dropping packed structs,
182
160
/// i.e., you do not usually have to worry about such issues unless you call `drop_in_place`
183
161
/// manually.
0 commit comments