about summary refs log tree commit diff
path: root/library/core/src
diff options
context:
space:
mode:
authorRyan Lopopolo <rjl@hyperbo.la>2021-05-15 10:37:05 -0700
committerRyan Lopopolo <rjl@hyperbo.la>2021-05-15 10:37:05 -0700
commit963bd3b643bae735cc46f8637e744163576244ea (patch)
treec69b682cff8fc07da81122363dcf40b94afb1e4f /library/core/src
parent2a245f40a19c9a60b3be33c959eb5cfb0ad163c6 (diff)
downloadrust-963bd3b643bae735cc46f8637e744163576244ea.tar.gz
rust-963bd3b643bae735cc46f8637e744163576244ea.zip
Implement more Iterator methods on core::iter::Repeat
`core::iter::Repeat` always returns the same element, which means we can
do better than implementing most `Iterator` methods in terms of
`Iterator::next`.

Fixes #81292.
Diffstat (limited to 'library/core/src')
-rw-r--r--library/core/src/iter/sources/repeat.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/library/core/src/iter/sources/repeat.rs b/library/core/src/iter/sources/repeat.rs
index d1f2879235f..a9478041c69 100644
--- a/library/core/src/iter/sources/repeat.rs
+++ b/library/core/src/iter/sources/repeat.rs
@@ -72,10 +72,32 @@ impl<A: Clone> Iterator for Repeat<A> {
     fn next(&mut self) -> Option<A> {
         Some(self.element.clone())
     }
+
     #[inline]
     fn size_hint(&self) -> (usize, Option<usize>) {
         (usize::MAX, None)
     }
+
+    #[inline]
+    fn advance_by(&mut self, n: usize) -> Result<(), usize> {
+        // Advancing an infinite iterator of a single element is a no-op.
+        let _ = n;
+        Ok(())
+    }
+
+    #[inline]
+    fn nth(&mut self, n: usize) -> Option<A> {
+        let _ = n;
+        Some(self.element.clone())
+    }
+
+    fn last(self) -> Option<A> {
+        loop {}
+    }
+
+    fn count(self) -> usize {
+        loop {}
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -84,6 +106,19 @@ impl<A: Clone> DoubleEndedIterator for Repeat<A> {
     fn next_back(&mut self) -> Option<A> {
         Some(self.element.clone())
     }
+
+    #[inline]
+    fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
+        // Advancing an infinite iterator of a single element is a no-op.
+        let _ = n;
+        Ok(())
+    }
+
+    #[inline]
+    fn nth_back(&mut self, n: usize) -> Option<A> {
+        let _ = n;
+        Some(self.element.clone())
+    }
 }
 
 #[stable(feature = "fused", since = "1.26.0")]