Auto merge of #77201 - matthewjasper:rename-get-unchecked, r=spastorino
Rename Iterator::get_unchecked Closes #76479 r? `@pnkfelix`
This commit is contained in:
commit
043f6d747c
|
@ -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)) }
|
||||
}
|
||||
|
|
|
@ -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() },
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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() }
|
||||
}
|
||||
}
|
||||
|
@ -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)) }
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
{
|
||||
|
|
|
@ -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`
|
||||
|
@ -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(),
|
||||
|
@ -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
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -318,7 +318,7 @@ macro_rules! iterator {
|
|||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
unsafe fn get_unchecked(&mut self, idx: usize) -> Self::Item {
|
||||
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
|
||||
// SAFETY: the caller must guarantee that `i` is in bounds of
|
||||
// the underlying slice, so `i` cannot overflow an `isize`, and
|
||||
// the returned references is guaranteed to refer to an element
|
||||
|
|
|
@ -813,10 +813,10 @@ impl Iterator for Bytes<'_> {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn get_unchecked(&mut self, idx: usize) -> u8 {
|
||||
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> u8 {
|
||||
// SAFETY: the caller must uphold the safety contract
|
||||
// for `Iterator::get_unchecked`.
|
||||
unsafe { self.0.get_unchecked(idx) }
|
||||
// for `Iterator::__iterator_get_unchecked`.
|
||||
unsafe { self.0.__iterator_get_unchecked(idx) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue