about summary refs log tree commit diff
path: root/src/libcore/slice
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-07-08 12:03:06 +0000
committerbors <bors@rust-lang.org>2019-07-08 12:03:06 +0000
commit78ca1bda3522b14bc0336bc01dd1d49fdba2cda7 (patch)
tree5813defd55c883f4bd34ef618a3c93bc12042185 /src/libcore/slice
parent10840b8ae2182128f2e1c84b8aaf41452728de07 (diff)
parent98b54fbd7afd55f40531bdb350a5adb870958111 (diff)
downloadrust-78ca1bda3522b14bc0336bc01dd1d49fdba2cda7.tar.gz
rust-78ca1bda3522b14bc0336bc01dd1d49fdba2cda7.zip
Auto merge of #62473 - timvermeulen:is_sorted_by_key, r=scottmcm
Only call the closure parameter of Iterator::is_sorted_by_key once per item

See https://github.com/rust-lang/rust/issues/53485#issuecomment-472314004.

This changes `Iterator::is_sorted_by_key` to only call the given closure once for each item, which allows us to pass the items to the closure by value instead of by reference.

**Important**: `is_sorted_by_key` for slices and slice iterators is now no longer implemented in terms of the custom `slice::Iter::is_sorted_by` implementation. It's a trade-off: we could forward `slice::Iter::is_sorted_by_key` to it directly for potential SIMD benefits, but that would mean that the closure is potentially called twice for (almost) every element of the slice.
Diffstat (limited to 'src/libcore/slice')
-rw-r--r--src/libcore/slice/mod.rs4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs
index fe48e2458cd..fdf3cc8e006 100644
--- a/src/libcore/slice/mod.rs
+++ b/src/libcore/slice/mod.rs
@@ -2459,12 +2459,12 @@ impl<T> [T] {
     /// ```
     #[inline]
     #[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
-    pub fn is_sorted_by_key<F, K>(&self, mut f: F) -> bool
+    pub fn is_sorted_by_key<F, K>(&self, f: F) -> bool
     where
         F: FnMut(&T) -> K,
         K: PartialOrd
     {
-        self.is_sorted_by(|a, b| f(a).partial_cmp(&f(b)))
+        self.iter().is_sorted_by_key(f)
     }
 }