Skip to content

Commit 88caa29

Browse files
authored
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`
2 parents 023079f + c2ccdfa commit 88caa29

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

library/core/src/slice/iter.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ iterator! {struct Iter -> *const T, &'a T, const, {/* no mut */}, {
132132
Self: Sized,
133133
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
134134
{
135-
self.as_slice().windows(2).all(|w| {
136-
compare(&&w[0], &&w[1]).map(|o| o != Ordering::Greater).unwrap_or(false)
137-
})
135+
self.as_slice().is_sorted_by(|a, b| compare(&a, &b))
138136
}
139137
}}
140138

library/core/src/slice/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3822,7 +3822,7 @@ impl<T> [T] {
38223822
where
38233823
F: FnMut(&'a T, &'a T) -> Option<Ordering>,
38243824
{
3825-
self.iter().is_sorted_by(|a, b| compare(*a, *b))
3825+
self.array_windows().all(|[a, b]| compare(a, b).map_or(false, Ordering::is_le))
38263826
}
38273827

38283828
/// Checks if the elements of this slice are sorted using the given key extraction function.

0 commit comments

Comments
 (0)