about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2020-10-02 08:25:22 +0900
committerGitHub <noreply@github.com>2020-10-02 08:25:22 +0900
commit2e749ab5a49290a9414cfcd7fd37be1b012e596a (patch)
tree696c80b75551760e5f7593aef1209606252dabfe
parentb97334f65ed5092e0172fd2fa01fd6c47e5a1841 (diff)
parente58f3d352d1c6f0ccc0b089754939bbcb7a2c294 (diff)
downloadrust-2e749ab5a49290a9414cfcd7fd37be1b012e596a.tar.gz
rust-2e749ab5a49290a9414cfcd7fd37be1b012e596a.zip
Rollup merge of #77385 - scottmcm:fix-77220, r=jyn514
Improve the example for ptr::copy

Fixes #77220
-rw-r--r--library/core/src/intrinsics.rs12
1 files changed, 11 insertions, 1 deletions
diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs
index 243fc7bfaa5..4e4b31c0cb4 100644
--- a/library/core/src/intrinsics.rs
+++ b/library/core/src/intrinsics.rs
@@ -1901,11 +1901,21 @@ pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
 /// ```
 /// use std::ptr;
 ///
+/// /// # Safety:
+/// /// * `ptr` must be correctly aligned for its type and non-zero.
+/// /// * `ptr` must be valid for reads of `elts` contiguous objects of type `T`.
+/// /// * Those elements must not be used after calling this function unless `T: Copy`.
 /// # #[allow(dead_code)]
 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
 ///     let mut dst = Vec::with_capacity(elts);
-///     dst.set_len(elts);
+///
+///     // SAFETY: Our precondition ensures the source is aligned and valid,
+///     // and `Vec::with_capacity` ensures that we have usable space to write them.
 ///     ptr::copy(ptr, dst.as_mut_ptr(), elts);
+///
+///     // SAFETY: We created it with this much capacity earlier,
+///     // and the previous `copy` has initialized these elements.
+///     dst.set_len(elts);
 ///     dst
 /// }
 /// ```