From 0d53565b60c54bb3f88ec711e87d45a87502de73 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 17 Mar 2023 18:04:56 +0000 Subject: [PATCH 1/2] Make `slice::is_sorted_by` impl nicer --- library/core/src/slice/iter.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index c4317799bcc68..68c7c5f23c974 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, { - self.as_slice().windows(2).all(|w| { - compare(&&w[0], &&w[1]).map(|o| o != Ordering::Greater).unwrap_or(false) - }) + self.as_slice().array_windows().all(|[a, b]| compare(&a, &b).map_or(false, Ordering::is_le)) } }} From c2ccdfa198be20d49b1ab55cb64855a4c9d33bfb Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 17 Mar 2023 18:10:21 +0000 Subject: [PATCH 2/2] Switch impls of `is_sorted_by` between slices and slice iters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes a bit more sense — iter impl converts to slice first, while slice impl used to create iter, doing unnecessary conversions. --- library/core/src/slice/iter.rs | 2 +- library/core/src/slice/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 68c7c5f23c974..88b84bd1352cc 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -132,7 +132,7 @@ iterator! {struct Iter -> *const T, &'a T, const, {/* no mut */}, { Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Option, { - self.as_slice().array_windows().all(|[a, b]| compare(&a, &b).map_or(false, Ordering::is_le)) + 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 d319b2bc37fdd..57b6e0ce4bb23 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -3822,7 +3822,7 @@ impl [T] { where F: FnMut(&'a T, &'a T) -> Option, { - 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.