Skip to content

Commit 323a279

Browse files
committed
Improve <vec::IntoIter>::get_unchecked` safety comment
1 parent 5b9e886 commit 323a279

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

library/alloc/src/vec.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2985,8 +2985,14 @@ impl<T> Iterator for IntoIter<T> {
29852985
where
29862986
Self: TrustedRandomAccess,
29872987
{
2988-
// SAFETY: the caller must uphold the contract for
2989-
// `Iterator::get_unchecked`.
2988+
// SAFETY: the caller must guarantee that `i` is in bounds of the
2989+
// `Vec<T>`, so `i` cannot overflow an `isize`, and the `self.ptr.add(i)`
2990+
// is guaranteed to pointer to an element of the `Vec<T>` and
2991+
// thus guaranteed to be valid to dereference.
2992+
//
2993+
// Also note the implementation of `Self: TrustedRandomAccess` requires
2994+
// that `T: Copy` so reading elements from the buffer doesn't invalidate
2995+
// them for `Drop`.
29902996
unsafe {
29912997
if mem::size_of::<T>() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) }
29922998
}

0 commit comments

Comments
 (0)