Skip to content

Commit

Permalink
Improve <vec::IntoIter>::get_unchecked` safety comment
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Sep 25, 2020
1 parent 5b9e886 commit 323a279
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions library/alloc/src/vec.rs
Expand Up @@ -2985,8 +2985,14 @@ impl<T> Iterator for IntoIter<T> {
where
Self: TrustedRandomAccess,
{
// SAFETY: the caller must uphold the contract for
// `Iterator::get_unchecked`.
// SAFETY: the caller must guarantee that `i` is in bounds of the
// `Vec<T>`, so `i` cannot overflow an `isize`, and the `self.ptr.add(i)`
// is guaranteed to pointer to an element of the `Vec<T>` and
// thus guaranteed to be valid to dereference.
//
// Also note the implementation of `Self: TrustedRandomAccess` requires
// that `T: Copy` so reading elements from the buffer doesn't invalidate
// them for `Drop`.
unsafe {
if mem::size_of::<T>() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) }
}
Expand Down

0 comments on commit 323a279

Please sign in to comment.