Skip to content

Commit 54f1124

Browse files
LukasKalbertodtkleimkuhler
authored andcommitted
Override Iterator::is_sorted_by in slice::Iter impl
Additionally, the root implementation was changed a bit: it now uses `all` instead of coding that logic manually. To avoid duplicate code, the inherent `[T]::is_sorted_by` method now calls `self.iter().is_sorted_by(...)`. This should always be inlined and not result in overhead.
1 parent 67729b4 commit 54f1124

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

Diff for: src/libcore/slice/mod.rs

+22-13
Original file line numberDiff line numberDiff line change
@@ -2293,16 +2293,7 @@ impl<T> [T] {
22932293
where
22942294
F: FnMut(&T, &T) -> Option<Ordering>
22952295
{
2296-
for pair in self.windows(2) {
2297-
if compare(&pair[0], &pair[1])
2298-
.map(|o| o == Ordering::Greater)
2299-
.unwrap_or(true)
2300-
{
2301-
return false;
2302-
}
2303-
}
2304-
2305-
true
2296+
self.iter().is_sorted_by(|a, b| compare(*a, *b))
23062297
}
23072298

23082299
/// Checks if the elements of this slice are sorted using the given key extraction function.
@@ -2853,7 +2844,13 @@ macro_rules! len {
28532844

28542845
// The shared definition of the `Iter` and `IterMut` iterators
28552846
macro_rules! iterator {
2856-
(struct $name:ident -> $ptr:ty, $elem:ty, $raw_mut:tt, $( $mut_:tt )*) => {
2847+
(
2848+
struct $name:ident -> $ptr:ty,
2849+
$elem:ty,
2850+
$raw_mut:tt,
2851+
{$( $mut_:tt )*},
2852+
{$($extra:tt)*}
2853+
) => {
28572854
impl<'a, T> $name<'a, T> {
28582855
// Helper function for creating a slice from the iterator.
28592856
#[inline(always)]
@@ -3030,6 +3027,8 @@ macro_rules! iterator {
30303027
i
30313028
})
30323029
}
3030+
3031+
$($extra)*
30333032
}
30343033

30353034
#[stable(feature = "rust1", since = "1.0.0")]
@@ -3167,7 +3166,17 @@ impl<'a, T> Iter<'a, T> {
31673166
}
31683167
}
31693168

3170-
iterator!{struct Iter -> *const T, &'a T, const, /* no mut */}
3169+
iterator!{struct Iter -> *const T, &'a T, const, {/* no mut */}, {
3170+
fn is_sorted_by<F>(self, mut compare: F) -> bool
3171+
where
3172+
Self: Sized,
3173+
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
3174+
{
3175+
self.as_slice().windows(2).all(|w| {
3176+
compare(&&w[0], &&w[1]).map(|o| o != Ordering::Greater).unwrap_or(false)
3177+
})
3178+
}
3179+
}}
31713180

31723181
#[stable(feature = "rust1", since = "1.0.0")]
31733182
impl<T> Clone for Iter<'_, T> {
@@ -3268,7 +3277,7 @@ impl<'a, T> IterMut<'a, T> {
32683277
}
32693278
}
32703279

3271-
iterator!{struct IterMut -> *mut T, &'a mut T, mut, mut}
3280+
iterator!{struct IterMut -> *mut T, &'a mut T, mut, {mut}, {}}
32723281

32733282
/// An internal abstraction over the splitting iterators, so that
32743283
/// splitn, splitn_mut etc can be implemented once.

Diff for: src/test/ui/feature-gates/feature-gate-is_sorted.rs

-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2-
// file at the top-level directory of this distribution and at
3-
// http://rust-lang.org/COPYRIGHT.
4-
//
5-
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6-
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7-
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8-
// option. This file may not be copied, modified, or distributed
9-
// except according to those terms.
10-
111
fn main() {
122
// Assert `Iterator` methods are feature gated
133
assert!([1, 2, 2, 9].iter().is_sorted());

Diff for: src/test/ui/feature-gates/feature-gate-is_sorted.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
2-
--> $DIR/feature-gate-is_sorted.rs:13:33
2+
--> $DIR/feature-gate-is_sorted.rs:3:33
33
|
44
LL | assert!([1, 2, 2, 9].iter().is_sorted());
55
| ^^^^^^^^^
66
|
77
= help: add #![feature(is_sorted)] to the crate attributes to enable
88

99
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
10-
--> $DIR/feature-gate-is_sorted.rs:15:39
10+
--> $DIR/feature-gate-is_sorted.rs:5:39
1111
|
1212
LL | assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
1313
| ^^^^^^^^^^^^^^^^
1414
|
1515
= help: add #![feature(is_sorted)] to the crate attributes to enable
1616

1717
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
18-
--> $DIR/feature-gate-is_sorted.rs:19:26
18+
--> $DIR/feature-gate-is_sorted.rs:9:26
1919
|
2020
LL | assert!([1, 2, 2, 9].is_sorted());
2121
| ^^^^^^^^^
2222
|
2323
= help: add #![feature(is_sorted)] to the crate attributes to enable
2424

2525
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
26-
--> $DIR/feature-gate-is_sorted.rs:21:32
26+
--> $DIR/feature-gate-is_sorted.rs:11:32
2727
|
2828
LL | assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
2929
| ^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)