@@ -106,7 +106,9 @@ pub use intrinsics::write_bytes;
106
106
///
107
107
/// * `to_drop` must be [valid] for reads.
108
108
///
109
- /// * `to_drop` must be properly aligned.
109
+ /// * `to_drop` must be properly aligned. See the example below for how to drop
110
+ /// an unaligned pointer.
111
+
110
112
///
111
113
/// Additionally, if `T` is not [`Copy`], using the pointed-to value after
112
114
/// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
@@ -137,6 +139,7 @@ pub use intrinsics::write_bytes;
137
139
/// // Without a call `drop_in_place`, the last item would never be dropped,
138
140
/// // and the memory it manages would be leaked.
139
141
/// ptr::drop_in_place(&mut v[1]);
142
+ /// // Shorten `v` to prevent the last item from being dropped.
140
143
/// v.set_len(1);
141
144
/// }
142
145
///
@@ -145,6 +148,31 @@ pub use intrinsics::write_bytes;
145
148
/// // Ensure that the last item was dropped.
146
149
/// assert!(weak.upgrade().is_none());
147
150
/// ```
151
+ ///
152
+ /// Drops a potentially unaligned value by copying it to aligned memory first:
153
+ /// ```
154
+ /// use std::ptr;
155
+ /// use std::mem;
156
+ ///
157
+ /// unsafe fn drop_after_copy<T>(to_drop: *mut T) {
158
+ /// let mut copy: T = mem::uninitialized();
159
+ /// let copy = &mut copy as *mut T;
160
+ /// ptr::copy(to_drop, copy, 1);
161
+ /// ptr::drop_in_place(copy);
162
+ /// }
163
+ ///
164
+ /// #[repr(packed, C)]
165
+ /// struct Packed {
166
+ /// _padding: u8,
167
+ /// unaligned: Vec<i32>,
168
+ /// }
169
+ ///
170
+ /// let mut p = Packed { _padding: 0, unaligned: vec![42] };
171
+ /// unsafe {
172
+ /// drop_after_copy(&mut p.unaligned as *mut _);
173
+ /// mem::forget(p);
174
+ /// }
175
+ /// ```
148
176
#[ stable( feature = "drop_in_place" , since = "1.8.0" ) ]
149
177
#[ lang = "drop_in_place" ]
150
178
#[ allow( unconditional_recursion) ]
@@ -601,7 +629,7 @@ pub unsafe fn read_unaligned<T>(src: *const T) -> T {
601
629
/// dropping the old value.
602
630
///
603
631
/// `write` does not drop the contents of `dst`. This is safe, but it could leak
604
- /// allocations or resources, so care must be taken not to overwrite an object
632
+ /// allocations or resources, so care should be taken not to overwrite an object
605
633
/// that should be dropped.
606
634
///
607
635
/// Additionally, it does not drop `src`. Semantically, `src` is moved into the
@@ -676,7 +704,7 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
676
704
/// Unlike [`write`], the pointer may be unaligned.
677
705
///
678
706
/// `write_unaligned` does not drop the contents of `dst`. This is safe, but it
679
- /// could leak allocations or resources, so care must be taken not to overwrite
707
+ /// could leak allocations or resources, so care should be taken not to overwrite
680
708
/// an object that should be dropped.
681
709
///
682
710
/// Additionally, it does not drop `src`. Semantically, `src` is moved into the
@@ -820,7 +848,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
820
848
/// [`read_volatile`].
821
849
///
822
850
/// `write_volatile` does not drop the contents of `dst`. This is safe, but it
823
- /// could leak allocations or resources, so care must be taken not to overwrite
851
+ /// could leak allocations or resources, so care should be taken not to overwrite
824
852
/// an object that should be dropped.
825
853
///
826
854
/// Additionally, it does not drop `src`. Semantically, `src` is moved into the
0 commit comments