未验证 提交 8f9f3aa0 编写于 作者: E Eric Huss 提交者: GitHub

Rollup merge of #89335 - mbrubeck:range-is-sorted, r=cuviper

Optimize is_sorted for Range and RangeInclusive

The [`Step`] trait guarantees that `Range<impl Step>` yields items in sorted order.  We can override `Iterator::is_sorted` based on this guarantee, as we already do for `Iterator::min` and `max`.

Thank you to ``@fiveseven-lambda`` who pointed this out [on the Rust Users Forum](https://users.rust-lang.org/t/is-sorted-method-in-impl-iterator-for-range/64717).

[`Step`]: https://doc.rust-lang.org/stable/std/iter/trait.Step.html
......@@ -672,6 +672,11 @@ fn max(mut self) -> Option<A> {
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 @@ fn min(mut self) -> Option<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")]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册