1
Fork 0

Auto merge of #77201 - matthewjasper:rename-get-unchecked, r=spastorino

Rename Iterator::get_unchecked

Closes #76479

r? `@pnkfelix`
This commit is contained in:
bors 2020-09-25 21:44:26 +00:00
commit 043f6d747c
8 changed files with 74 additions and 60 deletions

View file

@ -2980,12 +2980,18 @@ impl<T> Iterator for IntoIter<T> {
self.len()
}
unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item
unsafe fn __iterator_get_unchecked(&mut self, i: usize) -> Self::Item
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)) }
}