Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions library/alloc/src/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,12 @@ impl<T: Ord, A: Allocator> Iterator for IntoIterSorted<T, A> {
}

#[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
impl<T: Ord, A: Allocator> ExactSizeIterator for IntoIterSorted<T, A> {}
impl<T: Ord, A: Allocator> ExactSizeIterator for IntoIterSorted<T, A> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}

#[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
impl<T: Ord, A: Allocator> FusedIterator for IntoIterSorted<T, A> {}
Expand Down Expand Up @@ -1846,7 +1851,12 @@ impl<T: Ord, A: Allocator> Iterator for DrainSorted<'_, T, A> {
}

#[unstable(feature = "binary_heap_drain_sorted", issue = "59278")]
impl<T: Ord, A: Allocator> ExactSizeIterator for DrainSorted<'_, T, A> {}
impl<T: Ord, A: Allocator> ExactSizeIterator for DrainSorted<'_, T, A> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}

#[unstable(feature = "binary_heap_drain_sorted", issue = "59278")]
impl<T: Ord, A: Allocator> FusedIterator for DrainSorted<'_, T, A> {}
Expand Down
18 changes: 15 additions & 3 deletions library/alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,11 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for Iter<'_, T> {}
impl<T> ExactSizeIterator for Iter<'_, T> {
fn len(&self) -> usize {
self.len
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for Iter<'_, T> {}
Expand Down Expand Up @@ -1312,7 +1316,11 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for IterMut<'_, T> {}
impl<T> ExactSizeIterator for IterMut<'_, T> {
fn len(&self) -> usize {
self.len
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for IterMut<'_, T> {}
Expand Down Expand Up @@ -2023,7 +2031,11 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {}
impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
fn len(&self) -> usize {
self.list.len
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}
Expand Down
6 changes: 5 additions & 1 deletion library/alloc/src/collections/vec_deque/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
}

#[stable(feature = "drain", since = "1.6.0")]
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {}
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {
fn len(&self) -> usize {
self.remaining
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}
4 changes: 4 additions & 0 deletions library/alloc/src/vec/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {

#[stable(feature = "drain", since = "1.6.0")]
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {
fn len(&self) -> usize {
self.iter.len()
}

fn is_empty(&self) -> bool {
self.iter.is_empty()
}
Expand Down
6 changes: 5 additions & 1 deletion library/alloc/src/vec/splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ impl<I: Iterator, A: Allocator> DoubleEndedIterator for Splice<'_, I, A> {
}

#[stable(feature = "vec_splice", since = "1.21.0")]
impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> {}
impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> {
fn len(&self) -> usize {
self.drain.len()
}
}

#[stable(feature = "vec_splice", since = "1.21.0")]
impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> {
Expand Down
24 changes: 22 additions & 2 deletions library/core/src/iter/adapters/map_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,30 @@ impl<I: Iterator, const N: usize> MapWindowsInner<I, N> {
(lo, hi)
} else {
// If the first `N` items are not yet yielded by the inner iterator,
// the first `N` elements should be counted as one window, so both bounds
// should subtract `N - 1`.
// the first `N` elements should be counted as one window, so `N - 1` should
// subtracted from both bounds.
(lo.saturating_sub(N - 1), hi.map(|hi| hi.saturating_sub(N - 1)))
}
}
}

impl<I: ExactSizeIterator, const N: usize> MapWindowsInner<I, N> {
fn len(&self) -> usize {
let Some(ref iter) = self.iter else { return 0 };
let n = iter.len();
if self.buffer.is_some() {
// If the first `N` items are already yielded by the inner iterator,
// the length is then equal to the that of the inner iterator's.
n
} else {
// If the first `N` items are not yet yielded by the inner iterator,
// the first `N` elements should be counted as one window, so `N - 1` should
// be subtracted from the length.
n.saturating_sub(N - 1)
}
}
}

impl<T, const N: usize> Buffer<T, N> {
fn try_from_iter(iter: &mut impl Iterator<Item = T>) -> Option<Self> {
let first_half = crate::array::iter_next_chunk(iter).ok()?;
Expand Down Expand Up @@ -269,6 +286,9 @@ where
I: ExactSizeIterator,
F: FnMut(&[I::Item; N]) -> R,
{
fn len(&self) -> usize {
self.inner.len()
}
}

#[unstable(feature = "iter_map_windows", reason = "recently added", issue = "87155")]
Expand Down
8 changes: 7 additions & 1 deletion library/core/src/iter/adapters/peekable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,13 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I: ExactSizeIterator> ExactSizeIterator for Peekable<I> {}
impl<I: ExactSizeIterator> ExactSizeIterator for Peekable<I> {
#[inline]
fn len(&self) -> usize {
let peek_len = usize::from(matches!(self.peeked, Some(Some(_))));
self.iter.len().saturating_add(peek_len)
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<I: FusedIterator> FusedIterator for Peekable<I> {}
Expand Down
9 changes: 8 additions & 1 deletion library/core/src/iter/adapters/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> ExactSizeIterator for Skip<I> where I: ExactSizeIterator {}
impl<I> ExactSizeIterator for Skip<I>
where
I: ExactSizeIterator,
{
fn len(&self) -> usize {
self.iter.len().saturating_sub(self.n)
}
}

#[stable(feature = "double_ended_skip_iterator", since = "1.9.0")]
impl<I> DoubleEndedIterator for Skip<I>
Expand Down
9 changes: 8 additions & 1 deletion library/core/src/iter/adapters/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,14 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> ExactSizeIterator for Take<I> where I: ExactSizeIterator {}
impl<I> ExactSizeIterator for Take<I>
where
I: ExactSizeIterator,
{
fn len(&self) -> usize {
cmp::min(self.n, self.iter.len())
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<I> FusedIterator for Take<I> where I: FusedIterator {}
Expand Down
23 changes: 19 additions & 4 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ impl<T> Option<T> {
}

#[inline]
const fn len(&self) -> usize {
pub(crate) const fn len(&self) -> usize {
// Using the intrinsic avoids emitting a branch to get the 0 or 1.
let discriminant: isize = crate::intrinsics::discriminant_value(self);
discriminant as usize
Expand Down Expand Up @@ -2461,6 +2461,7 @@ impl<'a, A> Iterator for Iter<'a, A> {
fn next(&mut self) -> Option<&'a A> {
self.inner.next()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
Expand All @@ -2476,7 +2477,11 @@ impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A> ExactSizeIterator for Iter<'_, A> {}
impl<A> ExactSizeIterator for Iter<'_, A> {
fn len(&self) -> usize {
self.inner.len()
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<A> FusedIterator for Iter<'_, A> {}
Expand Down Expand Up @@ -2511,6 +2516,7 @@ impl<'a, A> Iterator for IterMut<'a, A> {
fn next(&mut self) -> Option<&'a mut A> {
self.inner.next()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
Expand All @@ -2526,7 +2532,11 @@ impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A> ExactSizeIterator for IterMut<'_, A> {}
impl<A> ExactSizeIterator for IterMut<'_, A> {
fn len(&self) -> usize {
self.inner.len()
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<A> FusedIterator for IterMut<'_, A> {}
Expand All @@ -2552,6 +2562,7 @@ impl<A> Iterator for IntoIter<A> {
fn next(&mut self) -> Option<A> {
self.inner.next()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
Expand All @@ -2567,7 +2578,11 @@ impl<A> DoubleEndedIterator for IntoIter<A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A> ExactSizeIterator for IntoIter<A> {}
impl<A> ExactSizeIterator for IntoIter<A> {
fn len(&self) -> usize {
self.inner.len()
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<A> FusedIterator for IntoIter<A> {}
Expand Down
26 changes: 20 additions & 6 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1984,9 +1984,10 @@ impl<'a, T> Iterator for Iter<'a, T> {
fn next(&mut self) -> Option<&'a T> {
self.inner.take()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let n = if self.inner.is_some() { 1 } else { 0 };
let n = self.len();
(n, Some(n))
}
}
Expand All @@ -2000,7 +2001,11 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for Iter<'_, T> {}
impl<T> ExactSizeIterator for Iter<'_, T> {
fn len(&self) -> usize {
self.inner.len()
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for Iter<'_, T> {}
Expand Down Expand Up @@ -2035,7 +2040,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let n = if self.inner.is_some() { 1 } else { 0 };
let n = self.len();
(n, Some(n))
}
}
Expand All @@ -2049,7 +2054,11 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for IterMut<'_, T> {}
impl<T> ExactSizeIterator for IterMut<'_, T> {
fn len(&self) -> usize {
self.inner.len()
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for IterMut<'_, T> {}
Expand Down Expand Up @@ -2079,9 +2088,10 @@ impl<T> Iterator for IntoIter<T> {
fn next(&mut self) -> Option<T> {
self.inner.take()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let n = if self.inner.is_some() { 1 } else { 0 };
let n = self.len();
(n, Some(n))
}
}
Expand All @@ -2095,7 +2105,11 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for IntoIter<T> {}
impl<T> ExactSizeIterator for IntoIter<T> {
fn len(&self) -> usize {
self.inner.len()
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for IntoIter<T> {}
Expand Down
Loading
Loading