Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Iterator::get_unchecked #77201

Merged
merged 2 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions library/alloc/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2981,12 +2981,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)) }
}
Expand Down
5 changes: 3 additions & 2 deletions library/core/src/iter/adapters/fuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,13 @@ where
}

#[inline]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
where
Self: TrustedRandomAccess,
{
match self.iter {
// SAFETY: the caller must uphold the contract for `Iterator::get_unchecked`.
// SAFETY: the caller must uphold the contract for
// `Iterator::__iterator_get_unchecked`.
Some(ref mut iter) => unsafe { try_get_unchecked(iter, idx) },
// SAFETY: the caller asserts there is an item at `i`, so we're not exhausted.
None => unsafe { intrinsics::unreachable() },
Expand Down
16 changes: 8 additions & 8 deletions library/core/src/iter/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ where
self.it.count()
}

unsafe fn get_unchecked(&mut self, idx: usize) -> T
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
where
Self: TrustedRandomAccess,
{
// SAFETY: the caller must uphold the contract for
// `Iterator::get_unchecked`.
// `Iterator::__iterator_get_unchecked`.
*unsafe { try_get_unchecked(&mut self.it, idx) }
}
}
Expand Down Expand Up @@ -420,12 +420,12 @@ where
self.it.map(T::clone).fold(init, f)
}

unsafe fn get_unchecked(&mut self, idx: usize) -> T
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> T
where
Self: TrustedRandomAccess,
{
// SAFETY: the caller must uphold the contract for
// `Iterator::get_unchecked`.
// `Iterator::__iterator_get_unchecked`.
unsafe { try_get_unchecked(&mut self.it, idx).clone() }
}
}
Expand Down Expand Up @@ -935,12 +935,12 @@ where
self.iter.fold(init, map_fold(self.f, g))
}

unsafe fn get_unchecked(&mut self, idx: usize) -> B
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> B
where
Self: TrustedRandomAccess,
{
// SAFETY: the caller must uphold the contract for
// `Iterator::get_unchecked`.
// `Iterator::__iterator_get_unchecked`.
unsafe { (self.f)(try_get_unchecked(&mut self.iter, idx)) }
}
}
Expand Down Expand Up @@ -1431,12 +1431,12 @@ where
self.iter.fold(init, enumerate(self.count, fold))
}

unsafe fn get_unchecked(&mut self, idx: usize) -> <Self as Iterator>::Item
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> <Self as Iterator>::Item
where
Self: TrustedRandomAccess,
{
// SAFETY: the caller must uphold the contract for
// `Iterator::get_unchecked`.
// `Iterator::__iterator_get_unchecked`.
let value = unsafe { try_get_unchecked(&mut self.iter, idx) };
(Add::add(self.count, idx), value)
}
Expand Down
42 changes: 23 additions & 19 deletions library/core/src/iter/adapters/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ where
}

#[inline]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
where
Self: TrustedRandomAccess,
{
// SAFETY: `ZipImpl::get_unchecked` has same safety requirements as
// `Iterator::get_unchecked`.
// SAFETY: `ZipImpl::__iterator_get_unchecked` has same safety
// requirements as `Iterator::__iterator_get_unchecked`.
unsafe { ZipImpl::get_unchecked(self, idx) }
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ trait ZipImpl<A, B> {
where
A: DoubleEndedIterator + ExactSizeIterator,
B: DoubleEndedIterator + ExactSizeIterator;
// This has the same safety requirements as `Iterator::get_unchecked`
// This has the same safety requirements as `Iterator::__iterator_get_unchecked`
unsafe fn get_unchecked(&mut self, idx: usize) -> <Self as Iterator>::Item
where
Self: Iterator + TrustedRandomAccess;
Expand Down Expand Up @@ -197,12 +197,14 @@ where
let i = self.index;
self.index += 1;
// SAFETY: `i` is smaller than `self.len`, thus smaller than `self.a.len()` and `self.b.len()`
unsafe { Some((self.a.get_unchecked(i), self.b.get_unchecked(i))) }
unsafe {
Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
}
} else if A::may_have_side_effect() && self.index < self.a.size() {
// match the base implementation's potential side effects
// SAFETY: we just checked that `self.index` < `self.a.len()`
unsafe {
self.a.get_unchecked(self.index);
self.a.__iterator_get_unchecked(self.index);
}
self.index += 1;
None
Expand All @@ -229,13 +231,13 @@ where
// ensures that `end` is smaller than or equal to `self.len`,
// so `i` is also smaller than `self.len`.
unsafe {
self.a.get_unchecked(i);
self.a.__iterator_get_unchecked(i);
}
}
if B::may_have_side_effect() {
// SAFETY: same as above.
unsafe {
self.b.get_unchecked(i);
self.b.__iterator_get_unchecked(i);
}
}
}
Expand Down Expand Up @@ -277,7 +279,9 @@ where
let i = self.len;
// SAFETY: `i` is smaller than the previous value of `self.len`,
// which is also smaller than or equal to `self.a.len()` and `self.b.len()`
unsafe { Some((self.a.get_unchecked(i), self.b.get_unchecked(i))) }
unsafe {
Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
}
} else {
None
}
Expand All @@ -286,8 +290,8 @@ where
#[inline]
unsafe fn get_unchecked(&mut self, idx: usize) -> <Self as Iterator>::Item {
// SAFETY: the caller must uphold the contract for
// `Iterator::get_unchecked`.
unsafe { (self.a.get_unchecked(idx), self.b.get_unchecked(idx)) }
// `Iterator::__iterator_get_unchecked`.
unsafe { (self.a.__iterator_get_unchecked(idx), self.b.__iterator_get_unchecked(idx)) }
}
}

Expand Down Expand Up @@ -386,8 +390,8 @@ impl<A: Debug + TrustedRandomAccess, B: Debug + TrustedRandomAccess> ZipFmt<A, B
///
/// `size` may not be overridden.
///
/// `<Self as Iterator>::get_unchecked` must be safe to call provided the
/// following conditions are met.
/// `<Self as Iterator>::__iterator_get_unchecked` must be safe to call
/// provided the following conditions are met.
///
/// 1. `0 <= idx` and `idx < self.size()`.
/// 2. If `self: !Clone`, then `get_unchecked` is never called with the same
Expand All @@ -399,7 +403,7 @@ impl<A: Debug + TrustedRandomAccess, B: Debug + TrustedRandomAccess> ZipFmt<A, B
/// * `std::clone::Clone::clone`
/// * `std::iter::Iterator::size_hint()`
/// * `std::iter::Iterator::next_back()`
/// * `std::iter::Iterator::get_unchecked()`
/// * `std::iter::Iterator::__iterator_get_unchecked()`
/// * `std::iter::TrustedRandomAccess::size()`
///
/// Further, given that these conditions are met, it must guarantee that:
Expand All @@ -424,7 +428,7 @@ pub unsafe trait TrustedRandomAccess: Sized {
fn may_have_side_effect() -> bool;
}

/// Like `Iterator::get_unchecked`, but doesn't require the compiler to
/// Like `Iterator::__iterator_get_unchecked`, but doesn't require the compiler to
/// know that `U: TrustedRandomAccess`.
///
/// ## Safety
Expand All @@ -436,13 +440,13 @@ where
I: Iterator,
{
// SAFETY: the caller must uphold the contract for
// `Iterator::get_unchecked`.
// `Iterator::__iterator_get_unchecked`.
unsafe { it.try_get_unchecked(idx) }
}

unsafe trait SpecTrustedRandomAccess: Iterator {
/// If `Self: TrustedRandomAccess`, it must be safe to call a
/// `Iterator::get_unchecked(self, index)`.
/// `Iterator::__iterator_get_unchecked(self, index)`.
unsafe fn try_get_unchecked(&mut self, index: usize) -> Self::Item;
}

Expand All @@ -455,7 +459,7 @@ unsafe impl<I: Iterator> SpecTrustedRandomAccess for I {
unsafe impl<I: Iterator + TrustedRandomAccess> SpecTrustedRandomAccess for I {
unsafe fn try_get_unchecked(&mut self, index: usize) -> Self::Item {
// SAFETY: the caller must uphold the contract for
// `Iterator::get_unchecked`.
unsafe { self.get_unchecked(index) }
// `Iterator::__iterator_get_unchecked`.
unsafe { self.__iterator_get_unchecked(index) }
}
}
4 changes: 3 additions & 1 deletion library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3241,10 +3241,12 @@ pub trait Iterator {
}

/// See [TrustedRandomAccess]
// The unusual name is to avoid name collisions in method resolution
// see #76479.
#[inline]
#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
unsafe fn get_unchecked(&mut self, _idx: usize) -> Self::Item
unsafe fn __iterator_get_unchecked(&mut self, _idx: usize) -> Self::Item
where
Self: TrustedRandomAccess,
{
Expand Down
47 changes: 24 additions & 23 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ impl<'a, T> Iterator for Windows<'a, T> {
}

#[doc(hidden)]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
// SAFETY: since the caller guarantees that `i` is in bounds,
// which means that `i` cannot overflow an `isize`, and the
// slice created by `from_raw_parts` is a subslice of `self.v`
Expand Down Expand Up @@ -1324,7 +1324,7 @@ impl<'a, T> Iterator for Chunks<'a, T> {
}

#[doc(hidden)]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
let start = idx * self.chunk_size;
let end = match start.checked_add(self.chunk_size) {
None => self.v.len(),
Expand Down Expand Up @@ -1480,13 +1480,13 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
}

#[doc(hidden)]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
let start = idx * self.chunk_size;
let end = match start.checked_add(self.chunk_size) {
None => self.v.len(),
Some(end) => cmp::min(end, self.v.len()),
};
// SAFETY: see comments for `Chunks::get_unchecked`.
// SAFETY: see comments for `Chunks::__iterator_get_unchecked`.
//
// Also note that the caller also guarantees that we're never called
// with the same index again, and that no other methods that will
Expand Down Expand Up @@ -1642,9 +1642,9 @@ impl<'a, T> Iterator for ChunksExact<'a, T> {
}

#[doc(hidden)]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
let start = idx * self.chunk_size;
// SAFETY: mostly identical to `Chunks::get_unchecked`.
// SAFETY: mostly identical to `Chunks::__iterator_get_unchecked`.
unsafe { from_raw_parts(self.v.as_ptr().add(start), self.chunk_size) }
}
}
Expand Down Expand Up @@ -1785,9 +1785,9 @@ impl<'a, T> Iterator for ChunksExactMut<'a, T> {
}

#[doc(hidden)]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
let start = idx * self.chunk_size;
// SAFETY: see comments for `ChunksMut::get_unchecked`.
// SAFETY: see comments for `ChunksMut::__iterator_get_unchecked`.
unsafe { from_raw_parts_mut(self.v.as_mut_ptr().add(start), self.chunk_size) }
}
}
Expand Down Expand Up @@ -2030,10 +2030,10 @@ impl<'a, T, const N: usize> Iterator for ArrayChunks<'a, T, N> {
self.iter.last()
}

unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T; N] {
// SAFETY: The safety guarantees of `get_unchecked` are transferred to
// the caller.
unsafe { self.iter.get_unchecked(i) }
unsafe fn __iterator_get_unchecked(&mut self, i: usize) -> &'a [T; N] {
// SAFETY: The safety guarantees of `__iterator_get_unchecked` are
// transferred to the caller.
unsafe { self.iter.__iterator_get_unchecked(i) }
}
}

Expand Down Expand Up @@ -2141,10 +2141,10 @@ impl<'a, T, const N: usize> Iterator for ArrayChunksMut<'a, T, N> {
self.iter.last()
}

unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut [T; N] {
// SAFETY: The safety guarantees of `get_unchecked` are transferred to
unsafe fn __iterator_get_unchecked(&mut self, i: usize) -> &'a mut [T; N] {
// SAFETY: The safety guarantees of `__iterator_get_unchecked` are transferred to
// the caller.
unsafe { self.iter.get_unchecked(i) }
unsafe { self.iter.__iterator_get_unchecked(i) }
}
}

Expand Down Expand Up @@ -2278,13 +2278,13 @@ impl<'a, T> Iterator for RChunks<'a, T> {
}

#[doc(hidden)]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
let end = self.v.len() - idx * self.chunk_size;
let start = match end.checked_sub(self.chunk_size) {
None => 0,
Some(start) => start,
};
// SAFETY: mostly identical to `Chunks::get_unchecked`.
// SAFETY: mostly identical to `Chunks::__iterator_get_unchecked`.
unsafe { from_raw_parts(self.v.as_ptr().add(start), end - start) }
}
}
Expand Down Expand Up @@ -2431,13 +2431,14 @@ impl<'a, T> Iterator for RChunksMut<'a, T> {
}

#[doc(hidden)]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
let end = self.v.len() - idx * self.chunk_size;
let start = match end.checked_sub(self.chunk_size) {
None => 0,
Some(start) => start,
};
// SAFETY: see comments for `RChunks::get_unchecked` and `ChunksMut::get_unchecked`
// SAFETY: see comments for `RChunks::__iterator_get_unchecked` and
// `ChunksMut::__iterator_get_unchecked`
unsafe { from_raw_parts_mut(self.v.as_mut_ptr().add(start), end - start) }
}
}
Expand Down Expand Up @@ -2585,11 +2586,11 @@ impl<'a, T> Iterator for RChunksExact<'a, T> {
}

#[doc(hidden)]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
let end = self.v.len() - idx * self.chunk_size;
let start = end - self.chunk_size;
// SAFETY:
// SAFETY: mostmy identical to `Chunks::get_unchecked`.
// SAFETY: mostmy identical to `Chunks::__iterator_get_unchecked`.
unsafe { from_raw_parts(self.v.as_ptr().add(start), self.chunk_size) }
}
}
Expand Down Expand Up @@ -2734,10 +2735,10 @@ impl<'a, T> Iterator for RChunksExactMut<'a, T> {
}

#[doc(hidden)]
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
let end = self.v.len() - idx * self.chunk_size;
let start = end - self.chunk_size;
// SAFETY: see comments for `RChunksMut::get_unchecked`.
// SAFETY: see comments for `RChunksMut::__iterator_get_unchecked`.
unsafe { from_raw_parts_mut(self.v.as_mut_ptr().add(start), self.chunk_size) }
}
}
Expand Down
Loading