Skip to content

Commit 3c047a2

Browse files
Change is_sorted[_by_key] methods to require Ord instead of PartialOrd
With `f32::total_cmp` and `f64::total_cmp`, the main reason for having an `PartialOrd` bound on these methods is mostly gone. By requiring `Ord`, we have no more edges cases with non-comparable values that need to be explained in the docs. These methods are now also in line with the `sort*` methods. Also note that `is_sorted` is mostly for convenience; if it is not powerful enough, there are more powerful tools that are only slightly less convenient. Finally, the trait bound can be relaxed in the future.
1 parent cb32cfb commit 3c047a2

File tree

4 files changed

+5
-15
lines changed

4 files changed

+5
-15
lines changed

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -3272,9 +3272,6 @@ pub trait Iterator {
32723272
/// That is, for each element `a` and its following element `b`, `a <= b` must hold. If the
32733273
/// iterator yields exactly zero or one element, `true` is returned.
32743274
///
3275-
/// Note that if `Self::Item` is only `PartialOrd`, but not `Ord`, the above definition
3276-
/// implies that this function returns `false` if any two consecutive items are not
3277-
/// comparable.
32783275
///
32793276
/// # Examples
32803277
///
@@ -3285,16 +3282,15 @@ pub trait Iterator {
32853282
/// assert!(![1, 3, 2, 4].iter().is_sorted());
32863283
/// assert!([0].iter().is_sorted());
32873284
/// assert!(std::iter::empty::<i32>().is_sorted());
3288-
/// assert!(![0.0, 1.0, f32::NAN].iter().is_sorted());
32893285
/// ```
32903286
#[inline]
32913287
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
32923288
fn is_sorted(self) -> bool
32933289
where
32943290
Self: Sized,
3295-
Self::Item: PartialOrd,
3291+
Self::Item: Ord,
32963292
{
3297-
self.is_sorted_by(PartialOrd::partial_cmp)
3293+
self.is_sorted_by(|a, b| Some(a.cmp(b)))
32983294
}
32993295

33003296
/// Checks if the elements of this iterator are sorted using the given comparator function.

library/core/src/slice/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -3322,9 +3322,6 @@ impl<T> [T] {
33223322
/// That is, for each element `a` and its following element `b`, `a <= b` must hold. If the
33233323
/// slice yields exactly zero or one element, `true` is returned.
33243324
///
3325-
/// Note that if `Self::Item` is only `PartialOrd`, but not `Ord`, the above definition
3326-
/// implies that this function returns `false` if any two consecutive items are not
3327-
/// comparable.
33283325
///
33293326
/// # Examples
33303327
///
@@ -3336,15 +3333,14 @@ impl<T> [T] {
33363333
/// assert!(![1, 3, 2, 4].is_sorted());
33373334
/// assert!([0].is_sorted());
33383335
/// assert!(empty.is_sorted());
3339-
/// assert!(![0.0, 1.0, f32::NAN].is_sorted());
33403336
/// ```
33413337
#[inline]
33423338
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
33433339
pub fn is_sorted(&self) -> bool
33443340
where
3345-
T: PartialOrd,
3341+
T: Ord,
33463342
{
3347-
self.is_sorted_by(|a, b| a.partial_cmp(b))
3343+
self.is_sorted_by(|a, b| Some(a.cmp(b)))
33483344
}
33493345

33503346
/// Checks if the elements of this slice are sorted using the given comparator function.
@@ -3383,7 +3379,7 @@ impl<T> [T] {
33833379
pub fn is_sorted_by_key<F, K>(&self, f: F) -> bool
33843380
where
33853381
F: FnMut(&T) -> K,
3386-
K: PartialOrd,
3382+
K: Ord,
33873383
{
33883384
self.iter().map(f).is_sorted()
33893385
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ fn test_is_sorted() {
373373
assert!(![1, 3, 2].iter().is_sorted());
374374
assert!([0].iter().is_sorted());
375375
assert!(std::iter::empty::<i32>().is_sorted());
376-
assert!(![0.0, 1.0, f32::NAN].iter().is_sorted());
377376
assert!([-2, -1, 0, 3].iter().is_sorted());
378377
assert!(!["c", "bb", "aaa"].iter().is_sorted());
379378
}

library/core/tests/slice.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,6 @@ fn test_is_sorted() {
20052005
assert!(![1, 3, 2].is_sorted());
20062006
assert!([0].is_sorted());
20072007
assert!(empty.is_sorted());
2008-
assert!(![0.0, 1.0, f32::NAN].is_sorted());
20092008
assert!([-2, -1, 0, 3].is_sorted());
20102009
assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
20112010
assert!(!["c", "bb", "aaa"].is_sorted());

0 commit comments

Comments
 (0)