where
F: FnMut(&T, &T) -> Option<Ordering>
{
- for pair in self.windows(2) {
- if compare(&pair[0], &pair[1])
- .map(|o| o == Ordering::Greater)
- .unwrap_or(true)
- {
- return false;
- }
- }
-
- true
+ self.iter().is_sorted_by(|a, b| compare(*a, *b))
}
/// Checks if the elements of this slice are sorted using the given key extraction function.
// The shared definition of the `Iter` and `IterMut` iterators
macro_rules! iterator {
- (struct $name:ident -> $ptr:ty, $elem:ty, $raw_mut:tt, $( $mut_:tt )*) => {
+ (
+ struct $name:ident -> $ptr:ty,
+ $elem:ty,
+ $raw_mut:tt,
+ {$( $mut_:tt )*},
+ {$($extra:tt)*}
+ ) => {
impl<'a, T> $name<'a, T> {
// Helper function for creating a slice from the iterator.
#[inline(always)]
i
})
}
+
+ $($extra)*
}
#[stable(feature = "rust1", since = "1.0.0")]
}
}
-iterator!{struct Iter -> *const T, &'a T, const, /* no mut */}
+iterator!{struct Iter -> *const T, &'a T, const, {/* no mut */}, {
+ fn is_sorted_by<F>(self, mut compare: F) -> bool
+ where
+ 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)
+ })
+ }
+}}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Clone for Iter<'_, T> {
}
}
-iterator!{struct IterMut -> *mut T, &'a mut T, mut, mut}
+iterator!{struct IterMut -> *mut T, &'a mut T, mut, {mut}, {}}
/// An internal abstraction over the splitting iterators, so that
/// splitn, splitn_mut etc can be implemented once.
-// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
fn main() {
// Assert `Iterator` methods are feature gated
assert!([1, 2, 2, 9].iter().is_sorted());
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
- --> $DIR/feature-gate-is_sorted.rs:13:33
+ --> $DIR/feature-gate-is_sorted.rs:3:33
|
LL | assert!([1, 2, 2, 9].iter().is_sorted());
| ^^^^^^^^^
= help: add #![feature(is_sorted)] to the crate attributes to enable
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
- --> $DIR/feature-gate-is_sorted.rs:15:39
+ --> $DIR/feature-gate-is_sorted.rs:5:39
|
LL | assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
| ^^^^^^^^^^^^^^^^
= help: add #![feature(is_sorted)] to the crate attributes to enable
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
- --> $DIR/feature-gate-is_sorted.rs:19:26
+ --> $DIR/feature-gate-is_sorted.rs:9:26
|
LL | assert!([1, 2, 2, 9].is_sorted());
| ^^^^^^^^^
= help: add #![feature(is_sorted)] to the crate attributes to enable
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
- --> $DIR/feature-gate-is_sorted.rs:21:32
+ --> $DIR/feature-gate-is_sorted.rs:11:32
|
LL | assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
| ^^^^^^^^^^^^^^^^