diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-03-20 09:46:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-20 09:46:53 +0100 |
| commit | 88caa29ae36f8d0f13b45c71368c3915e152156e (patch) | |
| tree | bff0eaef57a993ecae67bf0b4f9ab77205431acc | |
| parent | 023079fb862afbe3002646f545bb465009aa2579 (diff) | |
| parent | c2ccdfa198be20d49b1ab55cb64855a4c9d33bfb (diff) | |
| download | rust-88caa29ae36f8d0f13b45c71368c3915e152156e.tar.gz rust-88caa29ae36f8d0f13b45c71368c3915e152156e.zip | |
Rollup merge of #109273 - WaffleLapkin:slice_is_sorted_by_array_windows, r=scottmcm
Make `slice::is_sorted_by` implementation nicer Just tweak implementation a little :) r? `@thomcc`
| -rw-r--r-- | library/core/src/slice/iter.rs | 4 | ||||
| -rw-r--r-- | library/core/src/slice/mod.rs | 2 |
2 files changed, 2 insertions, 4 deletions
diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index c4317799bcc..88b84bd1352 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -132,9 +132,7 @@ iterator! {struct Iter -> *const T, &'a T, const, {/* no mut */}, { Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>, { - self.as_slice().windows(2).all(|w| { - compare(&&w[0], &&w[1]).map(|o| o != Ordering::Greater).unwrap_or(false) - }) + self.as_slice().is_sorted_by(|a, b| compare(&a, &b)) } }} diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index d319b2bc37f..57b6e0ce4bb 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -3822,7 +3822,7 @@ impl<T> [T] { where F: FnMut(&'a T, &'a T) -> Option<Ordering>, { - self.iter().is_sorted_by(|a, b| compare(*a, *b)) + self.array_windows().all(|[a, b]| compare(a, b).map_or(false, Ordering::is_le)) } /// Checks if the elements of this slice are sorted using the given key extraction function. |
