Skip to content

Commit 2713d36

Browse files
committed
tweaks
1 parent c44e88c commit 2713d36

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/libcore/intrinsics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ extern "rust-intrinsic" {
11181118
///
11191119
/// * `dst` must be properly aligned.
11201120
///
1121-
/// Additionally, the caller should ensure that writing `count *
1121+
/// Additionally, the caller must ensure that writing `count *
11221122
/// size_of::<T>()` bytes to the given region of memory results in a valid
11231123
/// value of `T`. Using a region of memory typed as a `T` that contains an
11241124
/// invalid value of `T` is undefined behavior.
@@ -1153,7 +1153,7 @@ extern "rust-intrinsic" {
11531153
/// unsafe {
11541154
/// // Leaks the previously held value by overwriting the `Box<T>` with
11551155
/// // a null pointer.
1156-
/// ptr::write_bytes(&mut v, 0, 1);
1156+
/// ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
11571157
/// }
11581158
///
11591159
/// // At this point, using or dropping `v` results in undefined behavior.
@@ -1164,7 +1164,7 @@ extern "rust-intrinsic" {
11641164
///
11651165
/// unsafe {
11661166
/// // Let us instead put in a valid value
1167-
/// ptr::write(&mut v, Box::new(42i32));
1167+
/// ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
11681168
/// }
11691169
///
11701170
/// // Now the box is fine

src/libcore/ptr.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)