@@ -117,7 +117,6 @@ pub use intrinsics::write_bytes;
117117///
118118/// * `to_drop` must be properly aligned. See the example below for how to drop
119119/// an unaligned pointer.
120-
121120///
122121/// Additionally, if `T` is not [`Copy`], using the pointed-to value after
123122/// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
@@ -185,6 +184,10 @@ pub use intrinsics::write_bytes;
185184/// mem::forget(p);
186185/// }
187186/// ```
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.
188191#[ stable( feature = "drop_in_place" , since = "1.8.0" ) ]
189192#[ lang = "drop_in_place" ]
190193#[ allow( unconditional_recursion) ]
@@ -547,6 +550,9 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
547550///
548551/// // Move `tmp` into `b`.
549552/// ptr::write(b, tmp);
553+ ///
554+ /// // `tmp` has been moved (`write` takes ownership of its second argument),
555+ /// // so nothing is dropped implicitly here.
550556/// }
551557/// }
552558///
@@ -688,9 +694,26 @@ pub unsafe fn read_unaligned<T>(src: *const T) -> T {
688694///
689695/// fn swap<T>(a: &mut T, b: &mut T) {
690696/// unsafe {
697+ /// // Create a bitwise copy of the value at `a` in `tmp`.
691698/// 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.
692707/// 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`.
693713/// ptr::write(b, tmp);
714+ ///
715+ /// // `tmp` has been moved (`write` takes ownership of its second argument),
716+ /// // so nothing is dropped implicitly here.
694717/// }
695718/// }
696719///
0 commit comments