Skip to content

Commit 564a5fb

Browse files
authored
Unrolled build for #118811
Rollup merge of #118811 - EbbDrop:is-sorted-by-bool, r=Mark-Simulacrum Use `bool` instead of `PartiolOrd` as return value of the comparison closure in `{slice,Iteraotr}::is_sorted_by` Changes the function signature of the closure given to `{slice,Iteraotr}::is_sorted_by` to return a `bool` instead of a `PartiolOrd` as suggested by the libs-api team here: #53485 (comment). This means these functions now return true if the closure returns true for all the pairs of values.
2 parents 867d39c + 606eeb8 commit 564a5fb

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed

Diff for: 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());
@@ -317,7 +317,7 @@ fn merge_codegen_units<'tcx>(
317317
assert!(cx.tcx.sess.codegen_units().as_usize() >= 1);
318318

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

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

Diff for: library/core/src/iter/traits/iterator.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -4033,42 +4033,42 @@ pub trait Iterator {
40334033
Self: Sized,
40344034
Self::Item: PartialOrd,
40354035
{
4036-
self.is_sorted_by(PartialOrd::partial_cmp)
4036+
self.is_sorted_by(|a, b| a <= b)
40374037
}
40384038

40394039
/// Checks if the elements of this iterator are sorted using the given comparator function.
40404040
///
40414041
/// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
4042-
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
4043-
/// [`is_sorted`]; see its documentation for more information.
4042+
/// function to determine whether two elements are to be considered in sorted order.
40444043
///
40454044
/// # Examples
40464045
///
40474046
/// ```
40484047
/// #![feature(is_sorted)]
40494048
///
4050-
/// assert!([1, 2, 2, 9].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
4051-
/// assert!(![1, 3, 2, 4].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
4052-
/// assert!([0].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
4053-
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| a.partial_cmp(b)));
4054-
/// assert!(![0.0, 1.0, f32::NAN].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
4055-
/// ```
4049+
/// assert!([1, 2, 2, 9].iter().is_sorted_by(|a, b| a <= b));
4050+
/// assert!(![1, 2, 2, 9].iter().is_sorted_by(|a, b| a < b));
40564051
///
4057-
/// [`is_sorted`]: Iterator::is_sorted
4052+
/// assert!([0].iter().is_sorted_by(|a, b| true));
4053+
/// assert!([0].iter().is_sorted_by(|a, b| false));
4054+
///
4055+
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| false));
4056+
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| true));
4057+
/// ```
40584058
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
40594059
#[rustc_do_not_const_check]
40604060
fn is_sorted_by<F>(mut self, compare: F) -> bool
40614061
where
40624062
Self: Sized,
4063-
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
4063+
F: FnMut(&Self::Item, &Self::Item) -> bool,
40644064
{
40654065
#[inline]
40664066
fn check<'a, T>(
40674067
last: &'a mut T,
4068-
mut compare: impl FnMut(&T, &T) -> Option<Ordering> + 'a,
4068+
mut compare: impl FnMut(&T, &T) -> bool + 'a,
40694069
) -> impl FnMut(T) -> bool + 'a {
40704070
move |curr| {
4071-
if let Some(Ordering::Greater) | None = compare(&last, &curr) {
4071+
if !compare(&last, &curr) {
40724072
return false;
40734073
}
40744074
*last = curr;

Diff for: 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
}

Diff for: library/core/src/slice/mod.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -3957,23 +3957,36 @@ impl<T> [T] {
39573957
where
39583958
T: PartialOrd,
39593959
{
3960-
self.is_sorted_by(|a, b| a.partial_cmp(b))
3960+
self.is_sorted_by(|a, b| a <= b)
39613961
}
39623962

39633963
/// Checks if the elements of this slice are sorted using the given comparator function.
39643964
///
39653965
/// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
3966-
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
3967-
/// [`is_sorted`]; see its documentation for more information.
3966+
/// function to determine whether two elements are to be considered in sorted order.
39683967
///
3969-
/// [`is_sorted`]: slice::is_sorted
3968+
/// # Examples
3969+
///
3970+
/// ```
3971+
/// #![feature(is_sorted)]
3972+
///
3973+
/// assert!([1, 2, 2, 9].is_sorted_by(|a, b| a <= b));
3974+
/// assert!(![1, 2, 2, 9].is_sorted_by(|a, b| a < b));
3975+
///
3976+
/// assert!([0].is_sorted_by(|a, b| true));
3977+
/// assert!([0].is_sorted_by(|a, b| false));
3978+
///
3979+
/// let empty: [i32; 0] = [];
3980+
/// assert!(empty.is_sorted_by(|a, b| false));
3981+
/// assert!(empty.is_sorted_by(|a, b| true));
3982+
/// ```
39703983
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
39713984
#[must_use]
39723985
pub fn is_sorted_by<'a, F>(&'a self, mut compare: F) -> bool
39733986
where
3974-
F: FnMut(&'a T, &'a T) -> Option<Ordering>,
3987+
F: FnMut(&'a T, &'a T) -> bool,
39753988
{
3976-
self.array_windows().all(|[a, b]| compare(a, b).map_or(false, Ordering::is_le))
3989+
self.array_windows().all(|[a, b]| compare(a, b))
39773990
}
39783991

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

Diff for: 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
@@ -408,7 +407,7 @@ fn test_is_sorted() {
408407

409408
// Tests for is_sorted_by
410409
assert!(![6, 2, 8, 5, 1, -60, 1337].iter().is_sorted());
411-
assert!([6, 2, 8, 5, 1, -60, 1337].iter().is_sorted_by(|_, _| Some(Ordering::Less)));
410+
assert!([6, 2, 8, 5, 1, -60, 1337].iter().is_sorted_by(|_, _| true));
412411

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

Diff for: 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)