Skip to content

Commit a396240

Browse files
committed
Rollup merge of #102977 - lukas-code:is-sorted-hrtb, r=m-ou-se
remove HRTB from `[T]::is_sorted_by{,_key}` Changes the signature of `[T]::is_sorted_by{,_key}` to match `[T]::binary_search_by{,_key}` and make code like #53485 (comment) compile. Tracking issue: #53485 ~~Do we need an ACP for something like this?~~ Edit: Filed ACP here: rust-lang/libs-team#121
2 parents 9cab6ca + f3d7b39 commit a396240

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

library/core/src/slice/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3752,9 +3752,9 @@ impl<T> [T] {
37523752
/// [`is_sorted`]: slice::is_sorted
37533753
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
37543754
#[must_use]
3755-
pub fn is_sorted_by<F>(&self, mut compare: F) -> bool
3755+
pub fn is_sorted_by<'a, F>(&'a self, mut compare: F) -> bool
37563756
where
3757-
F: FnMut(&T, &T) -> Option<Ordering>,
3757+
F: FnMut(&'a T, &'a T) -> Option<Ordering>,
37583758
{
37593759
self.iter().is_sorted_by(|a, b| compare(*a, *b))
37603760
}
@@ -3778,9 +3778,9 @@ impl<T> [T] {
37783778
#[inline]
37793779
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
37803780
#[must_use]
3781-
pub fn is_sorted_by_key<F, K>(&self, f: F) -> bool
3781+
pub fn is_sorted_by_key<'a, F, K>(&'a self, f: F) -> bool
37823782
where
3783-
F: FnMut(&T) -> K,
3783+
F: FnMut(&'a T) -> K,
37843784
K: PartialOrd,
37853785
{
37863786
self.iter().is_sorted_by_key(f)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// check-pass
2+
// regression test for https://github.com/rust-lang/rust/issues/53485#issuecomment-885393452
3+
4+
#![feature(is_sorted)]
5+
6+
struct A {
7+
name: String,
8+
}
9+
10+
fn main() {
11+
let a = &[
12+
A {
13+
name: "1".to_string(),
14+
},
15+
A {
16+
name: "2".to_string(),
17+
},
18+
];
19+
assert!(a.is_sorted_by_key(|a| a.name.as_str()));
20+
}

0 commit comments

Comments
 (0)