about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2022-11-18 17:48:16 -0500
committerGitHub <noreply@github.com>2022-11-18 17:48:16 -0500
commit8aca6ccedd0916da959638e6d2aac610d11e79c5 (patch)
tree3dd002a36395cd4f3d145b1adde338317a071016
parent70fe5f08fffd16dc20506f7d140e47b074f77964 (diff)
parentf3d7b39cdf483ae543757fbef369c166650e66f2 (diff)
downloadrust-8aca6ccedd0916da959638e6d2aac610d11e79c5.tar.gz
rust-8aca6ccedd0916da959638e6d2aac610d11e79c5.zip
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 https://github.com/rust-lang/rust/issues/53485#issuecomment-885393452 compile.

Tracking issue: https://github.com/rust-lang/rust/issues/53485

~~Do we need an ACP for something like this?~~ Edit: Filed ACP here: https://github.com/rust-lang/libs-team/issues/121
-rw-r--r--library/core/src/slice/mod.rs8
-rw-r--r--src/test/ui/array-slice-vec/slice_is_sorted_by_borrow.rs20
2 files changed, 24 insertions, 4 deletions
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs
index 0f58bc643d9..ad90e00b8a0 100644
--- a/library/core/src/slice/mod.rs
+++ b/library/core/src/slice/mod.rs
@@ -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))
     }
@@ -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)
diff --git a/src/test/ui/array-slice-vec/slice_is_sorted_by_borrow.rs b/src/test/ui/array-slice-vec/slice_is_sorted_by_borrow.rs
new file mode 100644
index 00000000000..073280d0fab
--- /dev/null
+++ b/src/test/ui/array-slice-vec/slice_is_sorted_by_borrow.rs
@@ -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()));
+}