@@ -3019,6 +3019,28 @@ macro_rules! iterator {
3019
3019
{ $( $mut_: tt ) * } ,
3020
3020
{ $( $extra: tt) * }
3021
3021
) => {
3022
+ // Returns the first element and moves the start of the iterator forwards by 1.
3023
+ // Greatly improves performance compared to an inlined function. The iterator
3024
+ // must not be empty.
3025
+ macro_rules! next_unchecked {
3026
+ ( $self: ident) => { & $( $mut_ ) * * $self. post_inc_start( 1 ) }
3027
+ }
3028
+
3029
+ // Returns the last element and moves the end of the iterator backwards by 1.
3030
+ // Greatly improves performance compared to an inlined function. The iterator
3031
+ // must not be empty.
3032
+ macro_rules! next_back_unchecked {
3033
+ ( $self: ident) => { & $( $mut_ ) * * $self. pre_dec_end( 1 ) }
3034
+ }
3035
+
3036
+ // Shrinks the iterator when T is a ZST, by moving the end of the iterator
3037
+ // backwards by `n`. `n` must not exceed `self.len()`.
3038
+ macro_rules! zst_shrink {
3039
+ ( $self: ident, $n: ident) => {
3040
+ $self. end = ( $self. end as * $raw_mut u8 ) . wrapping_offset( -$n) as * $raw_mut T ;
3041
+ }
3042
+ }
3043
+
3022
3044
impl <' a, T > $name<' a, T > {
3023
3045
// Helper function for creating a slice from the iterator.
3024
3046
#[ inline( always) ]
@@ -3028,12 +3050,11 @@ macro_rules! iterator {
3028
3050
3029
3051
// Helper function for moving the start of the iterator forwards by `offset` elements,
3030
3052
// returning the old start.
3031
- // Unsafe because the offset must be in-bounds or one-past-the-end .
3053
+ // Unsafe because the offset must not exceed `self.len()` .
3032
3054
#[ inline( always) ]
3033
3055
unsafe fn post_inc_start( & mut self , offset: isize ) -> * $raw_mut T {
3034
3056
if mem:: size_of:: <T >( ) == 0 {
3035
- // This is *reducing* the length. `ptr` never changes with ZST.
3036
- self . end = ( self . end as * $raw_mut u8 ) . wrapping_offset( -offset) as * $raw_mut T ;
3057
+ zst_shrink!( self , offset) ;
3037
3058
self . ptr
3038
3059
} else {
3039
3060
let old = self . ptr;
@@ -3044,11 +3065,11 @@ macro_rules! iterator {
3044
3065
3045
3066
// Helper function for moving the end of the iterator backwards by `offset` elements,
3046
3067
// returning the new end.
3047
- // Unsafe because the offset must be in-bounds or one-past-the-end .
3068
+ // Unsafe because the offset must not exceed `self.len()` .
3048
3069
#[ inline( always) ]
3049
3070
unsafe fn pre_dec_end( & mut self , offset: isize ) -> * $raw_mut T {
3050
3071
if mem:: size_of:: <T >( ) == 0 {
3051
- self . end = ( self . end as * $raw_mut u8 ) . wrapping_offset ( - offset) as * $raw_mut T ;
3072
+ zst_shrink! ( self , offset) ;
3052
3073
self . ptr
3053
3074
} else {
3054
3075
self . end = self . end. offset( -offset) ;
@@ -3085,7 +3106,7 @@ macro_rules! iterator {
3085
3106
if is_empty!( self ) {
3086
3107
None
3087
3108
} else {
3088
- Some ( & $ ( $mut_ ) * * self . post_inc_start ( 1 ) )
3109
+ Some ( next_unchecked! ( self ) )
3089
3110
}
3090
3111
}
3091
3112
}
@@ -3114,11 +3135,10 @@ macro_rules! iterator {
3114
3135
}
3115
3136
return None ;
3116
3137
}
3117
- // We are in bounds. `offset ` does the right thing even for ZSTs.
3138
+ // We are in bounds. `post_inc_start ` does the right thing even for ZSTs.
3118
3139
unsafe {
3119
- let elem = Some ( & $( $mut_ ) * * self . ptr. add( n) ) ;
3120
- self . post_inc_start( ( n as isize ) . wrapping_add( 1 ) ) ;
3121
- elem
3140
+ self . post_inc_start( n as isize ) ;
3141
+ Some ( next_unchecked!( self ) )
3122
3142
}
3123
3143
}
3124
3144
@@ -3135,13 +3155,13 @@ macro_rules! iterator {
3135
3155
let mut accum = init;
3136
3156
unsafe {
3137
3157
while len!( self ) >= 4 {
3138
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3139
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3140
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3141
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3158
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3159
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3160
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3161
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3142
3162
}
3143
3163
while !is_empty!( self ) {
3144
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3164
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3145
3165
}
3146
3166
}
3147
3167
Try :: from_ok( accum)
@@ -3212,11 +3232,25 @@ macro_rules! iterator {
3212
3232
if is_empty!( self ) {
3213
3233
None
3214
3234
} else {
3215
- Some ( & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) )
3235
+ Some ( next_back_unchecked! ( self ) )
3216
3236
}
3217
3237
}
3218
3238
}
3219
3239
3240
+ #[ inline]
3241
+ fn nth_back( & mut self , n: usize ) -> Option <$elem> {
3242
+ if n >= len!( self ) {
3243
+ // This iterator is now empty.
3244
+ self . end = self . ptr;
3245
+ return None ;
3246
+ }
3247
+ // We are in bounds. `pre_dec_end` does the right thing even for ZSTs.
3248
+ unsafe {
3249
+ self . pre_dec_end( n as isize ) ;
3250
+ Some ( next_back_unchecked!( self ) )
3251
+ }
3252
+ }
3253
+
3220
3254
#[ inline]
3221
3255
fn try_rfold<B , F , R >( & mut self , init: B , mut f: F ) -> R where
3222
3256
Self : Sized , F : FnMut ( B , Self :: Item ) -> R , R : Try <Ok =B >
@@ -3225,14 +3259,14 @@ macro_rules! iterator {
3225
3259
let mut accum = init;
3226
3260
unsafe {
3227
3261
while len!( self ) >= 4 {
3228
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3229
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3230
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3231
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3262
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3263
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3264
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3265
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3232
3266
}
3233
3267
// inlining is_empty everywhere makes a huge performance difference
3234
3268
while !is_empty!( self ) {
3235
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3269
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3236
3270
}
3237
3271
}
3238
3272
Try :: from_ok( accum)
0 commit comments