Skip to content

Commit 5a0a3af

Browse files
Rollup merge of rust-lang#151613 - cuviper:array-windows-parity, r=Amanieu
Align `ArrayWindows` trait impls with `Windows` With `slice::ArrayWindows` getting ready to stabilize in 1.94, I noticed that it currently has some differences in trait implementations compared to `slice::Windows`, and I think we should align these. - Remove `derive(Copy)` -- we generally don't want `Copy` for iterators at all, as this is seen as a footgun (e.g. rust-lang#21809). This is obviously a breaking change though, so we should only remove this if we also backport the removal before it's stable. Otherwise, it should at least be replaced by a manual impl without requiring `T: Copy`. - Manually `impl Clone`, simply to avoid requiring `T: Clone`. - `impl FusedIterator`, because it is trivially so. The `since = "1.94.0"` assumes we'll backport this, otherwise we should change that to the "current" placeholder. - `impl TrustedLen`, because we can trust our implementation. - `impl TrustedRandomAccess`, because the required `__iterator_get_unchecked` method is straightforward. r? libs-api @rustbot label beta-nominated (at least for the `Copy` removal, but we could be more selective about the rest).
2 parents f02f992 + 129d552 commit 5a0a3af

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

library/core/src/slice/iter.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,7 @@ unsafe impl<T> Sync for ChunksExactMut<'_, T> where T: Sync {}
21752175
///
21762176
/// [`array_windows`]: slice::array_windows
21772177
/// [slices]: slice
2178-
#[derive(Debug, Clone, Copy)]
2178+
#[derive(Debug)]
21792179
#[stable(feature = "array_windows", since = "1.94.0")]
21802180
#[must_use = "iterators are lazy and do nothing unless consumed"]
21812181
pub struct ArrayWindows<'a, T: 'a, const N: usize> {
@@ -2189,6 +2189,14 @@ impl<'a, T: 'a, const N: usize> ArrayWindows<'a, T, N> {
21892189
}
21902190
}
21912191

2192+
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
2193+
#[stable(feature = "array_windows", since = "1.94.0")]
2194+
impl<T, const N: usize> Clone for ArrayWindows<'_, T, N> {
2195+
fn clone(&self) -> Self {
2196+
Self { v: self.v }
2197+
}
2198+
}
2199+
21922200
#[stable(feature = "array_windows", since = "1.94.0")]
21932201
impl<'a, T, const N: usize> Iterator for ArrayWindows<'a, T, N> {
21942202
type Item = &'a [T; N];
@@ -2224,6 +2232,14 @@ impl<'a, T, const N: usize> Iterator for ArrayWindows<'a, T, N> {
22242232
fn last(self) -> Option<Self::Item> {
22252233
self.v.last_chunk()
22262234
}
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+
}
22272243
}
22282244

22292245
#[stable(feature = "array_windows", since = "1.94.0")]
@@ -2252,6 +2268,22 @@ impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N> {
22522268
}
22532269
}
22542270

2271+
#[unstable(feature = "trusted_len", issue = "37572")]
2272+
unsafe impl<T, const N: usize> TrustedLen for ArrayWindows<'_, T, N> {}
2273+
2274+
#[stable(feature = "array_windows", since = "1.94.0")]
2275+
impl<T, const N: usize> FusedIterator for ArrayWindows<'_, T, N> {}
2276+
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+
22552287
/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
22562288
/// time), starting at the end of the slice.
22572289
///

0 commit comments

Comments
 (0)