Skip to content

Commit

Permalink
Improve the example for ptr::copy
Browse files Browse the repository at this point in the history
Fixes #77220
  • Loading branch information
scottmcm committed Oct 1, 2020
1 parent 9bb55dc commit 20202da
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion library/core/src/intrinsics.rs
Expand Up @@ -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.
/// # #[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
/// }
/// ```
Expand Down

0 comments on commit 20202da

Please sign in to comment.