about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatt Brubeck <mbrubeck@limpet.net>2021-09-28 12:50:38 -0700
committerMatt Brubeck <mbrubeck@limpet.net>2021-09-28 12:50:38 -0700
commit830ecbd96ce30e3885663512ed94cbd347e64615 (patch)
tree1f5824c8a5ecec64f0d2d9fe54192b1f22dd669f
parent8f8092cc32ec171becef8ceacec7dbb06c5d7d7e (diff)
downloadrust-830ecbd96ce30e3885663512ed94cbd347e64615.tar.gz
rust-830ecbd96ce30e3885663512ed94cbd347e64615.zip
Optimize is_sorted for Range and RangeInclusive
The `Step` trait guarantees that `Range<impl Step>` yields items in
sorted order.  We can override the `Iterator::is_sorted` method based on
this guarantee, as we already do for `Iterator::min` and `max`.
-rw-r--r--library/core/src/iter/range.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/library/core/src/iter/range.rs b/library/core/src/iter/range.rs
index f7ed811e447..0f835689699 100644
--- a/library/core/src/iter/range.rs
+++ b/library/core/src/iter/range.rs
@@ -673,6 +673,11 @@ impl<A: Step> Iterator for ops::Range<A> {
     }
 
     #[inline]
+    fn is_sorted(self) -> bool {
+        true
+    }
+
+    #[inline]
     #[doc(hidden)]
     unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
     where
@@ -1095,6 +1100,11 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
     fn max(mut self) -> Option<A> {
         self.next_back()
     }
+
+    #[inline]
+    fn is_sorted(self) -> bool {
+        true
+    }
 }
 
 #[stable(feature = "inclusive_range", since = "1.26.0")]