Skip to content

Commit 129d552

Browse files
committed
impl TrustedRandomAccess for ArrayWindows
1 parent 8f7d556 commit 129d552

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

library/core/src/slice/iter.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,14 @@ impl<'a, T, const N: usize> Iterator for ArrayWindows<'a, T, N> {
22322232
fn last(self) -> Option<Self::Item> {
22332233
self.v.last_chunk()
22342234
}
2235+
2236+
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
2237+
// SAFETY: since the caller guarantees that `idx` is in bounds,
2238+
// which means that `idx` cannot overflow an `isize`, and the
2239+
// "slice" created by `cast_array` is a subslice of `self.v`
2240+
// thus is guaranteed to be valid for the lifetime `'a` of `self.v`.
2241+
unsafe { &*self.v.as_ptr().add(idx).cast_array() }
2242+
}
22352243
}
22362244

22372245
#[stable(feature = "array_windows", since = "1.94.0")]
@@ -2266,6 +2274,16 @@ unsafe impl<T, const N: usize> TrustedLen for ArrayWindows<'_, T, N> {}
22662274
#[stable(feature = "array_windows", since = "1.94.0")]
22672275
impl<T, const N: usize> FusedIterator for ArrayWindows<'_, T, N> {}
22682276

2277+
#[doc(hidden)]
2278+
#[unstable(feature = "trusted_random_access", issue = "none")]
2279+
unsafe impl<T, const N: usize> TrustedRandomAccess for ArrayWindows<'_, T, N> {}
2280+
2281+
#[doc(hidden)]
2282+
#[unstable(feature = "trusted_random_access", issue = "none")]
2283+
unsafe impl<T, const N: usize> TrustedRandomAccessNoCoerce for ArrayWindows<'_, T, N> {
2284+
const MAY_HAVE_SIDE_EFFECT: bool = false;
2285+
}
2286+
22692287
/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
22702288
/// time), starting at the end of the slice.
22712289
///

0 commit comments

Comments
 (0)