File tree 1 file changed +16
-9
lines changed
library/core/src/iter/traits
1 file changed +16
-9
lines changed Original file line number Diff line number Diff line change @@ -3317,24 +3317,31 @@ pub trait Iterator {
3317
3317
///
3318
3318
/// [`is_sorted`]: Iterator::is_sorted
3319
3319
#[ unstable( feature = "is_sorted" , reason = "new API" , issue = "53485" ) ]
3320
- fn is_sorted_by < F > ( mut self , mut compare : F ) -> bool
3320
+ fn is_sorted_by < F > ( mut self , compare : F ) -> bool
3321
3321
where
3322
3322
Self : Sized ,
3323
3323
F : FnMut ( & Self :: Item , & Self :: Item ) -> Option < Ordering > ,
3324
3324
{
3325
+ #[ inline]
3326
+ fn check < ' a , T > (
3327
+ last : & ' a mut T ,
3328
+ mut compare : impl FnMut ( & T , & T ) -> Option < Ordering > + ' a ,
3329
+ ) -> impl FnMut ( T ) -> bool + ' a {
3330
+ move |curr| {
3331
+ if let Some ( Ordering :: Greater ) | None = compare ( & last, & curr) {
3332
+ return false ;
3333
+ }
3334
+ * last = curr;
3335
+ true
3336
+ }
3337
+ }
3338
+
3325
3339
let mut last = match self . next ( ) {
3326
3340
Some ( e) => e,
3327
3341
None => return true ,
3328
3342
} ;
3329
3343
3330
- while let Some ( curr) = self . next ( ) {
3331
- if let Some ( Ordering :: Greater ) | None = compare ( & last, & curr) {
3332
- return false ;
3333
- }
3334
- last = curr;
3335
- }
3336
-
3337
- true
3344
+ self . all ( check ( & mut last, compare) )
3338
3345
}
3339
3346
3340
3347
/// Checks if the elements of this iterator are sorted using the given key extraction
You can’t perform that action at this time.
0 commit comments