@@ -117,7 +117,6 @@ pub use intrinsics::write_bytes;
117
117
///
118
118
/// * `to_drop` must be properly aligned. See the example below for how to drop
119
119
/// an unaligned pointer.
120
-
121
120
///
122
121
/// Additionally, if `T` is not [`Copy`], using the pointed-to value after
123
122
/// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
@@ -185,6 +184,10 @@ pub use intrinsics::write_bytes;
185
184
/// mem::forget(p);
186
185
/// }
187
186
/// ```
187
+ ///
188
+ /// Notice that the compiler performs this copy automatically when dropping packed structs,
189
+ /// i.e., you do not usually have to worry about such issues unless you call `drop_in_place`
190
+ /// manually.
188
191
#[ stable( feature = "drop_in_place" , since = "1.8.0" ) ]
189
192
#[ lang = "drop_in_place" ]
190
193
#[ allow( unconditional_recursion) ]
@@ -547,6 +550,9 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
547
550
///
548
551
/// // Move `tmp` into `b`.
549
552
/// ptr::write(b, tmp);
553
+ ///
554
+ /// // `tmp` has been moved (`write` takes ownership of its second argument),
555
+ /// // so nothing is dropped implicitly here.
550
556
/// }
551
557
/// }
552
558
///
@@ -688,9 +694,26 @@ pub unsafe fn read_unaligned<T>(src: *const T) -> T {
688
694
///
689
695
/// fn swap<T>(a: &mut T, b: &mut T) {
690
696
/// unsafe {
697
+ /// // Create a bitwise copy of the value at `a` in `tmp`.
691
698
/// let tmp = ptr::read(a);
699
+ ///
700
+ /// // Exiting at this point (either by explicitly returning or by
701
+ /// // calling a function which panics) would cause the value in `tmp` to
702
+ /// // be dropped while the same value is still referenced by `a`. This
703
+ /// // could trigger undefined behavior if `T` is not `Copy`.
704
+ ///
705
+ /// // Create a bitwise copy of the value at `b` in `a`.
706
+ /// // This is safe because mutable references cannot alias.
692
707
/// ptr::copy_nonoverlapping(b, a, 1);
708
+ ///
709
+ /// // As above, exiting here could trigger undefined behavior because
710
+ /// // the same value is referenced by `a` and `b`.
711
+ ///
712
+ /// // Move `tmp` into `b`.
693
713
/// ptr::write(b, tmp);
714
+ ///
715
+ /// // `tmp` has been moved (`write` takes ownership of its second argument),
716
+ /// // so nothing is dropped implicitly here.
694
717
/// }
695
718
/// }
696
719
///
0 commit comments