Skip to content

Commit cb32cfb

Browse files
Remove Iterator::is_sorted_by_key
This method can be trivially replaced by `iter.map(...).is_sorted()`. There has been some discussion about this, but the only reason for having that method that has been brought up is to have a more symmetry between the methods of `[T]` and `Iterator`. `[T]::is_sorted_by_key` is necessary, as there is not a simple replacement for it. But I don't think API symmetry is a strong enough argument for adding methods that are essentially superfluous or "a tiny bit convenient" at most. Finally, this method can always be added in the future. For the initial stabilization of the `is_sorted` API, it is not necessary.
1 parent d3163e9 commit cb32cfb

File tree

6 files changed

+14
-52
lines changed

6 files changed

+14
-52
lines changed

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
270270
tcx,
271271
arg,
272272
param,
273-
!args_iter.clone().is_sorted_by_key(|arg| match arg {
274-
GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
275-
GenericArg::Type(_) => ParamKindOrd::Type,
276-
GenericArg::Const(_) => ParamKindOrd::Const {
277-
unordered: tcx.features().const_generics,
278-
},
279-
}),
273+
!args_iter
274+
.clone()
275+
.map(|arg| match arg {
276+
GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
277+
GenericArg::Type(_) => ParamKindOrd::Type,
278+
GenericArg::Const(_) => ParamKindOrd::Const {
279+
unordered: tcx.features().const_generics,
280+
},
281+
})
282+
.is_sorted(),
280283
Some(&format!(
281284
"reorder the arguments: {}: `<{}>`",
282285
param_types_present

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,34 +3337,6 @@ pub trait Iterator {
33373337
true
33383338
}
33393339

3340-
/// Checks if the elements of this iterator are sorted using the given key extraction
3341-
/// function.
3342-
///
3343-
/// Instead of comparing the iterator's elements directly, this function compares the keys of
3344-
/// the elements, as determined by `f`. Apart from that, it's equivalent to [`is_sorted`]; see
3345-
/// its documentation for more information.
3346-
///
3347-
/// [`is_sorted`]: Iterator::is_sorted
3348-
///
3349-
/// # Examples
3350-
///
3351-
/// ```
3352-
/// #![feature(is_sorted)]
3353-
///
3354-
/// assert!(["c", "bb", "aaa"].iter().is_sorted_by_key(|s| s.len()));
3355-
/// assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
3356-
/// ```
3357-
#[inline]
3358-
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
3359-
fn is_sorted_by_key<F, K>(self, f: F) -> bool
3360-
where
3361-
Self: Sized,
3362-
F: FnMut(Self::Item) -> K,
3363-
K: PartialOrd,
3364-
{
3365-
self.map(f).is_sorted()
3366-
}
3367-
33683340
/// See [TrustedRandomAccess]
33693341
// The unusual name is to avoid name collisions in method resolution
33703342
// see #76479.

library/core/src/slice/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3385,7 +3385,7 @@ impl<T> [T] {
33853385
F: FnMut(&T) -> K,
33863386
K: PartialOrd,
33873387
{
3388-
self.iter().is_sorted_by_key(f)
3388+
self.iter().map(f).is_sorted()
33893389
}
33903390

33913391
/// Returns the index of the partition point according to the given predicate

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,7 @@ fn test_is_sorted() {
375375
assert!(std::iter::empty::<i32>().is_sorted());
376376
assert!(![0.0, 1.0, f32::NAN].iter().is_sorted());
377377
assert!([-2, -1, 0, 3].iter().is_sorted());
378-
assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
379378
assert!(!["c", "bb", "aaa"].iter().is_sorted());
380-
assert!(["c", "bb", "aaa"].iter().is_sorted_by_key(|s| s.len()));
381379
}
382380

383381
#[test]

src/test/ui/feature-gates/feature-gate-is_sorted.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ fn main() {
22
// Assert `Iterator` methods are unstable
33
assert!([1, 2, 2, 9].iter().is_sorted());
44
//~^ ERROR: use of unstable library feature 'is_sorted': new API
5-
assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
6-
//~^ ERROR: use of unstable library feature 'is_sorted': new API
75

86
// Assert `[T]` methods are unstable
97
assert!([1, 2, 2, 9].is_sorted());

src/test/ui/feature-gates/feature-gate-is_sorted.stderr

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ LL | assert!([1, 2, 2, 9].iter().is_sorted());
88
= help: add `#![feature(is_sorted)]` to the crate attributes to enable
99

1010
error[E0658]: use of unstable library feature 'is_sorted': new API
11-
--> $DIR/feature-gate-is_sorted.rs:5:39
12-
|
13-
LL | assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
14-
| ^^^^^^^^^^^^^^^^
15-
|
16-
= note: see issue #53485 <https://github.com/rust-lang/rust/issues/53485> for more information
17-
= help: add `#![feature(is_sorted)]` to the crate attributes to enable
18-
19-
error[E0658]: use of unstable library feature 'is_sorted': new API
20-
--> $DIR/feature-gate-is_sorted.rs:9:26
11+
--> $DIR/feature-gate-is_sorted.rs:7:26
2112
|
2213
LL | assert!([1, 2, 2, 9].is_sorted());
2314
| ^^^^^^^^^
@@ -26,14 +17,14 @@ LL | assert!([1, 2, 2, 9].is_sorted());
2617
= help: add `#![feature(is_sorted)]` to the crate attributes to enable
2718

2819
error[E0658]: use of unstable library feature 'is_sorted': new API
29-
--> $DIR/feature-gate-is_sorted.rs:11:32
20+
--> $DIR/feature-gate-is_sorted.rs:9:32
3021
|
3122
LL | assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
3223
| ^^^^^^^^^^^^^^^^
3324
|
3425
= note: see issue #53485 <https://github.com/rust-lang/rust/issues/53485> for more information
3526
= help: add `#![feature(is_sorted)]` to the crate attributes to enable
3627

37-
error: aborting due to 4 previous errors
28+
error: aborting due to 3 previous errors
3829

3930
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)