-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Improve the example for ptr::copy #77385
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I never heard of "contiguous objects"? Is this perhaps a new term? Also,
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try ctrl+f 'contiguous' on https://doc.rust-lang.org/nightly/std/vec/struct.Vec.html#guarantees There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, |
||||||
/// /// * Those elements must not be used after calling this function unless `T: Copy`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this required? Is it only limited for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, if these are not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I guess that's true but should this be part of safety? I think it could be but I wonder if this should be here. |
||||||
/// # #[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 | ||||||
/// } | ||||||
/// ``` | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually have a space after
Safety
for heading and don't have:
at the end.