Skip to content

Commit 606eeb8

Browse files
committed
Use bool instead of PartiolOrd in is_sorted_by
1 parent 7e452c1 commit 606eeb8

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed

compiler/rustc_monomorphize/src/partitioning.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ where
182182
}
183183

184184
// Ensure CGUs are sorted by name, so that we get deterministic results.
185-
if !codegen_units.is_sorted_by(|a, b| Some(a.name().as_str().cmp(b.name().as_str()))) {
185+
if !codegen_units.is_sorted_by(|a, b| a.name().as_str() <= b.name().as_str()) {
186186
let mut names = String::new();
187187
for cgu in codegen_units.iter() {
188188
names += &format!("- {}\n", cgu.name());
@@ -311,7 +311,7 @@ fn merge_codegen_units<'tcx>(
311311
assert!(cx.tcx.sess.codegen_units().as_usize() >= 1);
312312

313313
// A sorted order here ensures merging is deterministic.
314-
assert!(codegen_units.is_sorted_by(|a, b| Some(a.name().as_str().cmp(b.name().as_str()))));
314+
assert!(codegen_units.is_sorted_by(|a, b| a.name().as_str() <= b.name().as_str()));
315315

316316
// This map keeps track of what got merged into what.
317317
let mut cgu_contents: FxHashMap<Symbol, Vec<Symbol>> =

library/core/src/iter/traits/iterator.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -4026,42 +4026,42 @@ pub trait Iterator {
40264026
Self: Sized,
40274027
Self::Item: PartialOrd,
40284028
{
4029-
self.is_sorted_by(PartialOrd::partial_cmp)
4029+
self.is_sorted_by(|a, b| a <= b)
40304030
}
40314031

40324032
/// Checks if the elements of this iterator are sorted using the given comparator function.
40334033
///
40344034
/// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
4035-
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
4036-
/// [`is_sorted`]; see its documentation for more information.
4035+
/// function to determine whether two elements are to be considered in sorted order.
40374036
///
40384037
/// # Examples
40394038
///
40404039
/// ```
40414040
/// #![feature(is_sorted)]
40424041
///
4043-
/// assert!([1, 2, 2, 9].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
4044-
/// assert!(![1, 3, 2, 4].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
4045-
/// assert!([0].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
4046-
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| a.partial_cmp(b)));
4047-
/// assert!(![0.0, 1.0, f32::NAN].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
4048-
/// ```
4042+
/// assert!([1, 2, 2, 9].iter().is_sorted_by(|a, b| a <= b));
4043+
/// assert!(![1, 2, 2, 9].iter().is_sorted_by(|a, b| a < b));
40494044
///
4050-
/// [`is_sorted`]: Iterator::is_sorted
4045+
/// assert!([0].iter().is_sorted_by(|a, b| true));
4046+
/// assert!([0].iter().is_sorted_by(|a, b| false));
4047+
///
4048+
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| false));
4049+
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| true));
4050+
/// ```
40514051
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
40524052
#[rustc_do_not_const_check]
40534053
fn is_sorted_by<F>(mut self, compare: F) -> bool
40544054
where
40554055
Self: Sized,
4056-
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
4056+
F: FnMut(&Self::Item, &Self::Item) -> bool,
40574057
{
40584058
#[inline]
40594059
fn check<'a, T>(
40604060
last: &'a mut T,
4061-
mut compare: impl FnMut(&T, &T) -> Option<Ordering> + 'a,
4061+
mut compare: impl FnMut(&T, &T) -> bool + 'a,
40624062
) -> impl FnMut(T) -> bool + 'a {
40634063
move |curr| {
4064-
if let Some(Ordering::Greater) | None = compare(&last, &curr) {
4064+
if !compare(&last, &curr) {
40654065
return false;
40664066
}
40674067
*last = curr;

library/core/src/slice/iter.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
mod macros;
55

66
use crate::cmp;
7-
use crate::cmp::Ordering;
87
use crate::fmt;
98
use crate::intrinsics::assume;
109
use crate::iter::{
@@ -133,7 +132,7 @@ iterator! {struct Iter -> *const T, &'a T, const, {/* no mut */}, as_ref, {
133132
fn is_sorted_by<F>(self, mut compare: F) -> bool
134133
where
135134
Self: Sized,
136-
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
135+
F: FnMut(&Self::Item, &Self::Item) -> bool,
137136
{
138137
self.as_slice().is_sorted_by(|a, b| compare(&a, &b))
139138
}

library/core/src/slice/mod.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -4103,23 +4103,36 @@ impl<T> [T] {
41034103
where
41044104
T: PartialOrd,
41054105
{
4106-
self.is_sorted_by(|a, b| a.partial_cmp(b))
4106+
self.is_sorted_by(|a, b| a <= b)
41074107
}
41084108

41094109
/// Checks if the elements of this slice are sorted using the given comparator function.
41104110
///
41114111
/// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
4112-
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
4113-
/// [`is_sorted`]; see its documentation for more information.
4112+
/// function to determine whether two elements are to be considered in sorted order.
41144113
///
4115-
/// [`is_sorted`]: slice::is_sorted
4114+
/// # Examples
4115+
///
4116+
/// ```
4117+
/// #![feature(is_sorted)]
4118+
///
4119+
/// assert!([1, 2, 2, 9].is_sorted_by(|a, b| a <= b));
4120+
/// assert!(![1, 2, 2, 9].is_sorted_by(|a, b| a < b));
4121+
///
4122+
/// assert!([0].is_sorted_by(|a, b| true));
4123+
/// assert!([0].is_sorted_by(|a, b| false));
4124+
///
4125+
/// let empty: [i32; 0] = [];
4126+
/// assert!(empty.is_sorted_by(|a, b| false));
4127+
/// assert!(empty.is_sorted_by(|a, b| true));
4128+
/// ```
41164129
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
41174130
#[must_use]
41184131
pub fn is_sorted_by<'a, F>(&'a self, mut compare: F) -> bool
41194132
where
4120-
F: FnMut(&'a T, &'a T) -> Option<Ordering>,
4133+
F: FnMut(&'a T, &'a T) -> bool,
41214134
{
4122-
self.array_windows().all(|[a, b]| compare(a, b).map_or(false, Ordering::is_le))
4135+
self.array_windows().all(|[a, b]| compare(a, b))
41234136
}
41244137

41254138
/// Checks if the elements of this slice are sorted using the given key extraction function.

library/core/tests/iter/traits/iterator.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use core::cmp::Ordering;
21
use core::num::NonZeroUsize;
32

43
/// A wrapper struct that implements `Eq` and `Ord` based on the wrapped
@@ -402,7 +401,7 @@ fn test_is_sorted() {
402401

403402
// Tests for is_sorted_by
404403
assert!(![6, 2, 8, 5, 1, -60, 1337].iter().is_sorted());
405-
assert!([6, 2, 8, 5, 1, -60, 1337].iter().is_sorted_by(|_, _| Some(Ordering::Less)));
404+
assert!([6, 2, 8, 5, 1, -60, 1337].iter().is_sorted_by(|_, _| true));
406405

407406
// Tests for is_sorted_by_key
408407
assert!([-2, -1, 0, 3].iter().is_sorted());

library/core/tests/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,7 @@ fn test_is_sorted() {
23072307

23082308
// Tests for is_sorted_by
23092309
assert!(![6, 2, 8, 5, 1, -60, 1337].is_sorted());
2310-
assert!([6, 2, 8, 5, 1, -60, 1337].is_sorted_by(|_, _| Some(Ordering::Less)));
2310+
assert!([6, 2, 8, 5, 1, -60, 1337].is_sorted_by(|_, _| true));
23112311

23122312
// Tests for is_sorted_by_key
23132313
assert!([-2, -1, 0, 3].is_sorted());

0 commit comments

Comments
 (0)