@@ -70,21 +70,19 @@ macro_rules! iterator {
70
70
$into_ref: ident,
71
71
{ $( $extra: tt) * }
72
72
) => {
73
- // Returns the first element and moves the start of the iterator forwards by 1.
74
- // Greatly improves performance compared to an inlined function. The iterator
75
- // must not be empty.
76
- macro_rules! next_unchecked {
77
- ( $self: ident) => { $self. post_inc_start( 1 ) . $into_ref( ) }
78
- }
79
-
80
- // Returns the last element and moves the end of the iterator backwards by 1.
81
- // Greatly improves performance compared to an inlined function. The iterator
82
- // must not be empty.
83
- macro_rules! next_back_unchecked {
84
- ( $self: ident) => { $self. pre_dec_end( 1 ) . $into_ref( ) }
85
- }
86
-
87
73
impl <' a, T > $name<' a, T > {
74
+ /// Returns the last element and moves the end of the iterator backwards by 1.
75
+ ///
76
+ /// # Safety
77
+ ///
78
+ /// The iterator must not be empty
79
+ #[ inline]
80
+ unsafe fn next_back_unchecked( & mut self ) -> $elem {
81
+ // SAFETY: the caller promised it's not empty, so
82
+ // the offsetting is in-bounds and there's an element to return.
83
+ unsafe { self . pre_dec_end( 1 ) . $into_ref( ) }
84
+ }
85
+
88
86
// Helper function for creating a slice from the iterator.
89
87
#[ inline( always) ]
90
88
fn make_slice( & self ) -> & ' a [ T ] {
@@ -156,13 +154,13 @@ macro_rules! iterator {
156
154
fn next( & mut self ) -> Option <$elem> {
157
155
// could be implemented with slices, but this avoids bounds checks
158
156
159
- // SAFETY: The call to `next_unchecked! ` is
157
+ // SAFETY: The call to `next_unchecked` is
160
158
// safe since we check if the iterator is empty first.
161
159
unsafe {
162
160
if is_empty!( self ) {
163
161
None
164
162
} else {
165
- Some ( next_unchecked! ( self ) )
163
+ Some ( self . next_unchecked( ) )
166
164
}
167
165
}
168
166
}
@@ -191,7 +189,7 @@ macro_rules! iterator {
191
189
// SAFETY: We are in bounds. `post_inc_start` does the right thing even for ZSTs.
192
190
unsafe {
193
191
self . post_inc_start( n) ;
194
- Some ( next_unchecked! ( self ) )
192
+ Some ( self . next_unchecked( ) )
195
193
}
196
194
}
197
195
@@ -392,13 +390,13 @@ macro_rules! iterator {
392
390
fn next_back( & mut self ) -> Option <$elem> {
393
391
// could be implemented with slices, but this avoids bounds checks
394
392
395
- // SAFETY: The call to `next_back_unchecked! `
393
+ // SAFETY: The call to `next_back_unchecked`
396
394
// is safe since we check if the iterator is empty first.
397
395
unsafe {
398
396
if is_empty!( self ) {
399
397
None
400
398
} else {
401
- Some ( next_back_unchecked! ( self ) )
399
+ Some ( self . next_back_unchecked( ) )
402
400
}
403
401
}
404
402
}
@@ -416,7 +414,7 @@ macro_rules! iterator {
416
414
// SAFETY: We are in bounds. `pre_dec_end` does the right thing even for ZSTs.
417
415
unsafe {
418
416
self . pre_dec_end( n) ;
419
- Some ( next_back_unchecked! ( self ) )
417
+ Some ( self . next_back_unchecked( ) )
420
418
}
421
419
}
422
420
@@ -436,10 +434,11 @@ macro_rules! iterator {
436
434
unsafe impl <T > TrustedLen for $name<' _, T > { }
437
435
438
436
impl <' a, T > UncheckedIterator for $name<' a, T > {
437
+ #[ inline]
439
438
unsafe fn next_unchecked( & mut self ) -> $elem {
440
439
// SAFETY: The caller promised there's at least one more item.
441
440
unsafe {
442
- next_unchecked! ( self )
441
+ self . post_inc_start ( 1 ) . $into_ref ( )
443
442
}
444
443
}
445
444
}
0 commit comments