Skip to content

Commit f5f5105

Browse files
committed
Change ArrayWindows to use a slice
1 parent 4056082 commit f5f5105

File tree

1 file changed

+31
-46
lines changed

1 file changed

+31
-46
lines changed

library/core/src/slice/iter.rs

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,16 +2203,13 @@ unsafe impl<T> Sync for ChunksExactMut<'_, T> where T: Sync {}
22032203
#[unstable(feature = "array_windows", issue = "75027")]
22042204
#[must_use = "iterators are lazy and do nothing unless consumed"]
22052205
pub struct ArrayWindows<'a, T: 'a, const N: usize> {
2206-
slice_head: *const T,
2207-
num: usize,
2208-
marker: PhantomData<&'a [T; N]>,
2206+
v: &'a [T],
22092207
}
22102208

22112209
impl<'a, T: 'a, const N: usize> ArrayWindows<'a, T, N> {
22122210
#[inline]
22132211
pub(super) const fn new(slice: &'a [T]) -> Self {
2214-
let num_windows = slice.len().saturating_sub(N - 1);
2215-
Self { slice_head: slice.as_ptr(), num: num_windows, marker: PhantomData }
2212+
Self { v: slice }
22162213
}
22172214
}
22182215

@@ -2222,82 +2219,70 @@ impl<'a, T, const N: usize> Iterator for ArrayWindows<'a, T, N> {
22222219

22232220
#[inline]
22242221
fn next(&mut self) -> Option<Self::Item> {
2225-
if self.num == 0 {
2226-
return None;
2222+
if let Some(ret) = self.v.first_chunk() {
2223+
self.v = &self.v[1..];
2224+
Some(ret)
2225+
} else {
2226+
None
22272227
}
2228-
// SAFETY:
2229-
// This is safe because it's indexing into a slice guaranteed to be length > N.
2230-
let ret = unsafe { &*self.slice_head.cast::<[T; N]>() };
2231-
// SAFETY: Guaranteed that there are at least 1 item remaining otherwise
2232-
// earlier branch would've been hit
2233-
self.slice_head = unsafe { self.slice_head.add(1) };
2234-
2235-
self.num -= 1;
2236-
Some(ret)
22372228
}
22382229

22392230
#[inline]
22402231
fn size_hint(&self) -> (usize, Option<usize>) {
2241-
(self.num, Some(self.num))
2232+
if self.v.len() < N {
2233+
(0, Some(0))
2234+
} else {
2235+
let size = self.v.len() - N + 1;
2236+
(size, Some(size))
2237+
}
22422238
}
22432239

22442240
#[inline]
22452241
fn count(self) -> usize {
2246-
self.num
2242+
self.size_hint().0
22472243
}
22482244

22492245
#[inline]
22502246
fn nth(&mut self, n: usize) -> Option<Self::Item> {
2251-
if self.num <= n {
2252-
self.num = 0;
2253-
return None;
2254-
}
2255-
// SAFETY:
2256-
// This is safe because it's indexing into a slice guaranteed to be length > N.
2257-
let ret = unsafe { &*self.slice_head.add(n).cast::<[T; N]>() };
2258-
// SAFETY: Guaranteed that there are at least n items remaining
2259-
self.slice_head = unsafe { self.slice_head.add(n + 1) };
2260-
2261-
self.num -= n + 1;
2262-
Some(ret)
2247+
self.v = &self.v[n.min(self.v.len())..];
2248+
self.next()
22632249
}
22642250

22652251
#[inline]
2266-
fn last(mut self) -> Option<Self::Item> {
2267-
self.nth(self.num.checked_sub(1)?)
2252+
fn last(self) -> Option<Self::Item> {
2253+
self.v.last_chunk()
22682254
}
22692255
}
22702256

22712257
#[unstable(feature = "array_windows", issue = "75027")]
22722258
impl<'a, T, const N: usize> DoubleEndedIterator for ArrayWindows<'a, T, N> {
22732259
#[inline]
22742260
fn next_back(&mut self) -> Option<&'a [T; N]> {
2275-
if self.num == 0 {
2276-
return None;
2261+
if let Some(ret) = self.v.last_chunk() {
2262+
self.v = &self.v[..self.v.len() - 1];
2263+
Some(ret)
2264+
} else {
2265+
None
22772266
}
2278-
// SAFETY: Guaranteed that there are n items remaining, n-1 for 0-indexing.
2279-
let ret = unsafe { &*self.slice_head.add(self.num - 1).cast::<[T; N]>() };
2280-
self.num -= 1;
2281-
Some(ret)
22822267
}
22832268

22842269
#[inline]
22852270
fn nth_back(&mut self, n: usize) -> Option<&'a [T; N]> {
2286-
if self.num <= n {
2287-
self.num = 0;
2288-
return None;
2271+
if let Some(idx) = self.v.len().checked_sub(n)
2272+
&& self.v.split_off(idx..).is_some()
2273+
{
2274+
self.next_back()
2275+
} else {
2276+
self.v = &self.v[..0];
2277+
None
22892278
}
2290-
// SAFETY: Guaranteed that there are n items remaining, n-1 for 0-indexing.
2291-
let ret = unsafe { &*self.slice_head.add(self.num - (n + 1)).cast::<[T; N]>() };
2292-
self.num -= n + 1;
2293-
Some(ret)
22942279
}
22952280
}
22962281

22972282
#[unstable(feature = "array_windows", issue = "75027")]
22982283
impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N> {
22992284
fn is_empty(&self) -> bool {
2300-
self.num == 0
2285+
self.v.len() < N
23012286
}
23022287
}
23032288

0 commit comments

Comments
 (0)