Skip to content

Commit a31ad75

Browse files
authored
Auto merge of #37944 - bluss:adaptors-are-empty, r=alexcrichton
Forward ExactSizeIterator::len and is_empty for important iterator adaptors Forward ExactSizeIterator::len and is_empty for important iterator adaptors Because some iterators will provide improved version of len and/or is_empty, adaptors should forward to those implementations if possible.
2 parents ddf011d + 22739a1 commit a31ad75

File tree

1 file changed

+57
-6
lines changed

1 file changed

+57
-6
lines changed

src/libcore/iter/mod.rs

+57-6
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,16 @@ impl<I> DoubleEndedIterator for Rev<I> where I: DoubleEndedIterator {
368368

369369
#[stable(feature = "rust1", since = "1.0.0")]
370370
impl<I> ExactSizeIterator for Rev<I>
371-
where I: ExactSizeIterator + DoubleEndedIterator {}
371+
where I: ExactSizeIterator + DoubleEndedIterator
372+
{
373+
fn len(&self) -> usize {
374+
self.iter.len()
375+
}
376+
377+
fn is_empty(&self) -> bool {
378+
self.iter.is_empty()
379+
}
380+
}
372381

373382
#[unstable(feature = "fused", issue = "35602")]
374383
impl<I> FusedIterator for Rev<I>
@@ -425,7 +434,15 @@ impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
425434
#[stable(feature = "iter_cloned", since = "1.1.0")]
426435
impl<'a, I, T: 'a> ExactSizeIterator for Cloned<I>
427436
where I: ExactSizeIterator<Item=&'a T>, T: Clone
428-
{}
437+
{
438+
fn len(&self) -> usize {
439+
self.it.len()
440+
}
441+
442+
fn is_empty(&self) -> bool {
443+
self.it.is_empty()
444+
}
445+
}
429446

430447
#[unstable(feature = "fused", issue = "35602")]
431448
impl<'a, I, T: 'a> FusedIterator for Cloned<I>
@@ -1007,7 +1024,16 @@ impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for Map<I, F> where
10071024

10081025
#[stable(feature = "rust1", since = "1.0.0")]
10091026
impl<B, I: ExactSizeIterator, F> ExactSizeIterator for Map<I, F>
1010-
where F: FnMut(I::Item) -> B {}
1027+
where F: FnMut(I::Item) -> B
1028+
{
1029+
fn len(&self) -> usize {
1030+
self.iter.len()
1031+
}
1032+
1033+
fn is_empty(&self) -> bool {
1034+
self.iter.is_empty()
1035+
}
1036+
}
10111037

10121038
#[unstable(feature = "fused", issue = "35602")]
10131039
impl<B, I: FusedIterator, F> FusedIterator for Map<I, F>
@@ -1236,7 +1262,15 @@ impl<I> DoubleEndedIterator for Enumerate<I> where
12361262
}
12371263

12381264
#[stable(feature = "rust1", since = "1.0.0")]
1239-
impl<I> ExactSizeIterator for Enumerate<I> where I: ExactSizeIterator {}
1265+
impl<I> ExactSizeIterator for Enumerate<I> where I: ExactSizeIterator {
1266+
fn len(&self) -> usize {
1267+
self.iter.len()
1268+
}
1269+
1270+
fn is_empty(&self) -> bool {
1271+
self.iter.is_empty()
1272+
}
1273+
}
12401274

12411275
#[doc(hidden)]
12421276
unsafe impl<I> TrustedRandomAccess for Enumerate<I>
@@ -1945,7 +1979,15 @@ impl<I> DoubleEndedIterator for Fuse<I>
19451979

19461980

19471981
#[stable(feature = "rust1", since = "1.0.0")]
1948-
impl<I> ExactSizeIterator for Fuse<I> where I: ExactSizeIterator {}
1982+
impl<I> ExactSizeIterator for Fuse<I> where I: ExactSizeIterator {
1983+
fn len(&self) -> usize {
1984+
self.iter.len()
1985+
}
1986+
1987+
fn is_empty(&self) -> bool {
1988+
self.iter.is_empty()
1989+
}
1990+
}
19491991

19501992
/// An iterator that calls a function with a reference to each element before
19511993
/// yielding it.
@@ -2012,7 +2054,16 @@ impl<I: DoubleEndedIterator, F> DoubleEndedIterator for Inspect<I, F>
20122054

20132055
#[stable(feature = "rust1", since = "1.0.0")]
20142056
impl<I: ExactSizeIterator, F> ExactSizeIterator for Inspect<I, F>
2015-
where F: FnMut(&I::Item) {}
2057+
where F: FnMut(&I::Item)
2058+
{
2059+
fn len(&self) -> usize {
2060+
self.iter.len()
2061+
}
2062+
2063+
fn is_empty(&self) -> bool {
2064+
self.iter.is_empty()
2065+
}
2066+
}
20162067

20172068
#[unstable(feature = "fused", issue = "35602")]
20182069
impl<I: FusedIterator, F> FusedIterator for Inspect<I, F>

0 commit comments

Comments
 (0)