From 830ecbd96ce30e3885663512ed94cbd347e64615 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Tue, 28 Sep 2021 12:50:38 -0700 Subject: [PATCH] Optimize is_sorted for Range and RangeInclusive The `Step` trait guarantees that `Range` 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`. --- library/core/src/iter/range.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/core/src/iter/range.rs b/library/core/src/iter/range.rs index f7ed811e44733..0f835689699fc 100644 --- a/library/core/src/iter/range.rs +++ b/library/core/src/iter/range.rs @@ -672,6 +672,11 @@ impl Iterator for ops::Range { self.next_back() } + #[inline] + fn is_sorted(self) -> bool { + true + } + #[inline] #[doc(hidden)] unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item @@ -1095,6 +1100,11 @@ impl Iterator for ops::RangeInclusive { fn max(mut self) -> Option { self.next_back() } + + #[inline] + fn is_sorted(self) -> bool { + true + } } #[stable(feature = "inclusive_range", since = "1.26.0")]