Skip to content

Commit de546aa

Browse files
committed
Auto merge of rust-lang#147235 - matthiaskrgr:rollup-a0es1x9, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#146593 (Allow specifying multiple bounds for same associated item, except in trait objects) - rust-lang#147177 ([DebugInfo] Fix MSVC tuple child creation) - rust-lang#147195 (iter repeat: add tests for new count and last behavior) - rust-lang#147202 (Swap order of `resolve_coroutine_interiors` and `handle_opaque_type_uses`) - rust-lang#147204 (Refactor ArrayWindows to use a slice) - rust-lang#147219 (Add proper error handling for closure in impl) - rust-lang#147226 (include `outer_inclusive_binder` of pattern types) - rust-lang#147230 (Fix typo in 'unfulfilled_lint_expectation' to plural) r? `@ghost` `@rustbot` modify labels: rollup
2 parents cd3c095 + 294e962 commit de546aa

File tree

2 files changed

+33
-47
lines changed

2 files changed

+33
-47
lines changed

core/src/slice/iter.rs

Lines changed: 22 additions & 47 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,60 @@ 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+
let ret = self.v.first_chunk();
2223+
if ret.is_some() {
2224+
self.v = &self.v[1..];
22272225
}
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)
2226+
ret
22372227
}
22382228

22392229
#[inline]
22402230
fn size_hint(&self) -> (usize, Option<usize>) {
2241-
(self.num, Some(self.num))
2231+
let size = self.v.len().saturating_sub(N - 1);
2232+
(size, Some(size))
22422233
}
22432234

22442235
#[inline]
22452236
fn count(self) -> usize {
2246-
self.num
2237+
self.len()
22472238
}
22482239

22492240
#[inline]
22502241
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)
2242+
let idx = n.min(self.v.len());
2243+
self.v = &self.v[idx..];
2244+
self.next()
22632245
}
22642246

22652247
#[inline]
2266-
fn last(mut self) -> Option<Self::Item> {
2267-
self.nth(self.num.checked_sub(1)?)
2248+
fn last(self) -> Option<Self::Item> {
2249+
self.v.last_chunk()
22682250
}
22692251
}
22702252

22712253
#[unstable(feature = "array_windows", issue = "75027")]
22722254
impl<'a, T, const N: usize> DoubleEndedIterator for ArrayWindows<'a, T, N> {
22732255
#[inline]
22742256
fn next_back(&mut self) -> Option<&'a [T; N]> {
2275-
if self.num == 0 {
2276-
return None;
2257+
let ret = self.v.last_chunk();
2258+
if ret.is_some() {
2259+
self.v = &self.v[..self.v.len() - 1];
22772260
}
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)
2261+
ret
22822262
}
22832263

22842264
#[inline]
22852265
fn nth_back(&mut self, n: usize) -> Option<&'a [T; N]> {
2286-
if self.num <= n {
2287-
self.num = 0;
2288-
return None;
2289-
}
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)
2266+
let idx = self.v.len().saturating_sub(n);
2267+
self.v = &self.v[..idx];
2268+
self.next_back()
22942269
}
22952270
}
22962271

22972272
#[unstable(feature = "array_windows", issue = "75027")]
22982273
impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N> {
22992274
fn is_empty(&self) -> bool {
2300-
self.num == 0
2275+
self.v.len() < N
23012276
}
23022277
}
23032278

coretests/tests/iter/sources.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ fn test_repeat_take_collect() {
3030
assert_eq!(v, vec![42, 42, 42]);
3131
}
3232

33+
#[test]
34+
#[should_panic = "iterator is infinite"]
35+
fn test_repeat_count() {
36+
repeat(42).count();
37+
}
38+
39+
#[test]
40+
fn test_repeat_last() {
41+
assert_eq!(repeat(42).last(), Some(42));
42+
}
43+
3344
#[test]
3445
fn test_repeat_with() {
3546
#[derive(PartialEq, Debug)]

0 commit comments

Comments
 (0)