Skip to content

Commit 1ec66fb

Browse files
committed
apply comments
1 parent fc63113 commit 1ec66fb

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/libcore/intrinsics.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,10 +1024,9 @@ extern "rust-intrinsic" {
10241024
/// // not alias, and two different vectors cannot own the same
10251025
/// // memory.
10261026
/// ptr::copy_nonoverlapping(src, dst, src_len);
1027-
/// }
10281027
///
1029-
/// unsafe {
1030-
/// // Truncate `src` without dropping its contents.
1028+
/// // Truncate `src` without dropping its contents. This cannot panic,
1029+
/// // so double-drops cannot happen.
10311030
/// src.set_len(0);
10321031
///
10331032
/// // Notify `dst` that it now holds the contents of `src`.
@@ -1054,7 +1053,9 @@ extern "rust-intrinsic" {
10541053
/// If the source and destination will *never* overlap,
10551054
/// [`copy_nonoverlapping`] can be used instead.
10561055
///
1057-
/// `copy` is semantically equivalent to C's [`memmove`].
1056+
/// `copy` is semantically equivalent to C's [`memmove`]. Copying takes place as
1057+
/// if the bytes were copied from `src` to a temporary array and then copied from
1058+
/// the array to `dst`-
10581059
///
10591060
/// [`copy_nonoverlapping`]: ./fn.copy_nonoverlapping.html
10601061
/// [`memmove`]: https://www.gnu.org/software/libc/manual/html_node/Copying-Strings-and-Arrays.html#index-memmove
@@ -1143,7 +1144,7 @@ extern "rust-intrinsic" {
11431144
///
11441145
/// Creating an invalid value:
11451146
///
1146-
/// ```no_run
1147+
/// ```
11471148
/// use std::ptr;
11481149
///
11491150
/// let mut v = Box::new(0i32);
@@ -1155,7 +1156,10 @@ extern "rust-intrinsic" {
11551156
/// }
11561157
///
11571158
/// // At this point, using or dropping `v` results in undefined behavior.
1158-
/// // v = Box::new(0i32); // ERROR
1159+
/// // drop(v); // ERROR
1160+
///
1161+
/// // Leaking it does not invoke drop and is fine:
1162+
/// mem::forget(v)
11591163
/// ```
11601164
#[stable(feature = "rust1", since = "1.0.0")]
11611165
pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);

src/libcore/ptr.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
274274
/// size_of::<T>()` bytes must *not* overlap with the region of memory
275275
/// beginning at `y` with the same size.
276276
///
277-
/// Note that even if `T` has size `0`, the pointers must be non-NULL and properly aligned.
277+
/// Note that even if the effectively copied size (`count * size_of::<T>()`) is `0`,
278+
/// the pointers must be non-NULL and properly aligned.
278279
///
279280
/// [valid]: ../ptr/index.html#safety
280281
///
@@ -369,7 +370,7 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
369370
}
370371
}
371372

372-
/// Moves `src` into the pointed `dest`, returning the previous `dest` value.
373+
/// Moves `src` into the pointed `dst`, returning the previous `dst` value.
373374
///
374375
/// Neither value is dropped.
375376
///
@@ -383,9 +384,9 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
383384
///
384385
/// Behavior is undefined if any of the following conditions are violated:
385386
///
386-
/// * `dest` must be [valid] for writes.
387+
/// * `dst` must be [valid] for writes.
387388
///
388-
/// * `dest` must be properly aligned.
389+
/// * `dst` must be properly aligned.
389390
///
390391
/// Note that even if `T` has size `0`, the pointer must be non-NULL and properly aligned.
391392
///
@@ -409,8 +410,8 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
409410
/// ```
410411
#[inline]
411412
#[stable(feature = "rust1", since = "1.0.0")]
412-
pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
413-
mem::swap(&mut *dest, &mut src); // cannot overlap
413+
pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
414+
mem::swap(&mut *dst, &mut src); // cannot overlap
414415
src
415416
}
416417

@@ -447,8 +448,8 @@ pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
447448
///
448449
/// let mut s = String::from("foo");
449450
/// unsafe {
450-
/// // `s2` now points to the same underlying memory as `s1`.
451-
/// let mut s2 = ptr::read(&s);
451+
/// // `s2` now points to the same underlying memory as `s`.
452+
/// let mut s2: String = ptr::read(&s);
452453
///
453454
/// assert_eq!(s2, "foo");
454455
///
@@ -558,7 +559,6 @@ pub unsafe fn read<T>(src: *const T) -> T {
558559
/// use std::ptr;
559560
///
560561
/// #[repr(packed, C)]
561-
/// #[derive(Default)]
562562
/// struct Packed {
563563
/// _padding: u8,
564564
/// unaligned: u32,
@@ -570,10 +570,11 @@ pub unsafe fn read<T>(src: *const T) -> T {
570570
/// };
571571
///
572572
/// let v = unsafe {
573-
/// // Take a reference to a 32-bit integer which is not aligned.
574-
/// let unaligned = &x.unaligned;
573+
/// // Take the address of a 32-bit integer which is not aligned.
574+
/// // This must be done as a raw pointer; unaligned references are invalid.
575+
/// let unaligned = &x.unaligned as *const u32;
575576
///
576-
/// // Dereferencing normally will emit an unaligned load instruction,
577+
/// // Dereferencing normally will emit an aligned load instruction,
577578
/// // causing undefined behavior.
578579
/// // let v = *unaligned; // ERROR
579580
///

0 commit comments

Comments
 (0)