Skip to content

Commit

Permalink
Rollup merge of rust-lang#102977 - lukas-code:is-sorted-hrtb, r=m-ou-se
Browse files Browse the repository at this point in the history
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 rust-lang#53485 (comment) compile.

Tracking issue: rust-lang#53485

~~Do we need an ACP for something like this?~~ Edit: Filed ACP here: rust-lang/libs-team#121
  • Loading branch information
Manishearth authored Nov 18, 2022
2 parents 70fe5f0 + f3d7b39 commit 8aca6cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3752,9 +3752,9 @@ impl<T> [T] {
/// [`is_sorted`]: slice::is_sorted
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
#[must_use]
pub fn is_sorted_by<F>(&self, mut compare: F) -> bool
pub fn is_sorted_by<'a, F>(&'a self, mut compare: F) -> bool
where
F: FnMut(&T, &T) -> Option<Ordering>,
F: FnMut(&'a T, &'a T) -> Option<Ordering>,
{
self.iter().is_sorted_by(|a, b| compare(*a, *b))
}
Expand All @@ -3778,9 +3778,9 @@ impl<T> [T] {
#[inline]
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
#[must_use]
pub fn is_sorted_by_key<F, K>(&self, f: F) -> bool
pub fn is_sorted_by_key<'a, F, K>(&'a self, f: F) -> bool
where
F: FnMut(&T) -> K,
F: FnMut(&'a T) -> K,
K: PartialOrd,
{
self.iter().is_sorted_by_key(f)
Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/array-slice-vec/slice_is_sorted_by_borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// check-pass
// regression test for https://github.com/rust-lang/rust/issues/53485#issuecomment-885393452

#![feature(is_sorted)]

struct A {
name: String,
}

fn main() {
let a = &[
A {
name: "1".to_string(),
},
A {
name: "2".to_string(),
},
];
assert!(a.is_sorted_by_key(|a| a.name.as_str()));
}

0 comments on commit 8aca6cc

Please sign in to comment.