Skip to content

Commit 2e749ab

Browse files
authored
Rollup merge of #77385 - scottmcm:fix-77220, r=jyn514
Improve the example for ptr::copy Fixes #77220
2 parents b97334f + e58f3d3 commit 2e749ab

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

library/core/src/intrinsics.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1901,11 +1901,21 @@ pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
19011901
/// ```
19021902
/// use std::ptr;
19031903
///
1904+
/// /// # Safety:
1905+
/// /// * `ptr` must be correctly aligned for its type and non-zero.
1906+
/// /// * `ptr` must be valid for reads of `elts` contiguous objects of type `T`.
1907+
/// /// * Those elements must not be used after calling this function unless `T: Copy`.
19041908
/// # #[allow(dead_code)]
19051909
/// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
19061910
/// let mut dst = Vec::with_capacity(elts);
1907-
/// dst.set_len(elts);
1911+
///
1912+
/// // SAFETY: Our precondition ensures the source is aligned and valid,
1913+
/// // and `Vec::with_capacity` ensures that we have usable space to write them.
19081914
/// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1915+
///
1916+
/// // SAFETY: We created it with this much capacity earlier,
1917+
/// // and the previous `copy` has initialized these elements.
1918+
/// dst.set_len(elts);
19091919
/// dst
19101920
/// }
19111921
/// ```

0 commit comments

Comments
 (0)