@@ -2,11 +2,10 @@ use super::core::RingMapCore;
22use super :: { Bucket , Entries , RingMap } ;
33
44use alloc:: collections:: vec_deque:: { self , VecDeque } ;
5- use core:: fmt;
65use core:: hash:: { BuildHasher , Hash } ;
76use core:: iter:: FusedIterator ;
87use core:: ops:: { Index , RangeBounds } ;
9- use core:: slice;
8+ use core:: { fmt , mem , slice} ;
109
1110impl < ' a , K , V , S > IntoIterator for & ' a RingMap < K , V , S > {
1211 type Item = ( & ' a K , & ' a V ) ;
@@ -64,7 +63,12 @@ impl<'a, K, V> Iterator for Buckets<'a, K, V> {
6463 fn next ( & mut self ) -> Option < Self :: Item > {
6564 match self . head . next ( ) {
6665 next @ Some ( _) => next,
67- None => self . tail . next ( ) ,
66+ None => {
67+ // Swap so the rest is found on the first branch next time.
68+ // (Like `VecDeque` does in its own iterators.)
69+ mem:: swap ( & mut self . head , & mut self . tail ) ;
70+ self . head . next ( )
71+ }
6872 }
6973 }
7074
@@ -78,14 +82,12 @@ impl<'a, K, V> Iterator for Buckets<'a, K, V> {
7882 }
7983
8084 fn nth ( & mut self , mut n : usize ) -> Option < Self :: Item > {
81- if n < self . head . len ( ) {
82- return self . head . nth ( n) ;
83- }
84- if self . head . len ( ) > 0 {
85+ if n >= self . head . len ( ) {
8586 n -= self . head . len ( ) ;
8687 self . head = [ ] . iter ( ) ;
88+ mem:: swap ( & mut self . head , & mut self . tail ) ;
8789 }
88- self . tail . nth ( n)
90+ self . head . nth ( n)
8991 }
9092
9193 fn last ( mut self ) -> Option < Self :: Item > {
@@ -112,19 +114,22 @@ impl<K, V> DoubleEndedIterator for Buckets<'_, K, V> {
112114 fn next_back ( & mut self ) -> Option < Self :: Item > {
113115 match self . tail . next_back ( ) {
114116 next @ Some ( _) => next,
115- None => self . head . next_back ( ) ,
117+ None => {
118+ // Swap so the rest is found on the first branch next time.
119+ // (Like `VecDeque` does in its own iterators.)
120+ mem:: swap ( & mut self . head , & mut self . tail ) ;
121+ self . tail . next_back ( )
122+ }
116123 }
117124 }
118125
119126 fn nth_back ( & mut self , mut n : usize ) -> Option < Self :: Item > {
120- if n < self . tail . len ( ) {
121- return self . tail . nth_back ( n) ;
122- }
123- if self . tail . len ( ) > 0 {
127+ if n >= self . tail . len ( ) {
124128 n -= self . tail . len ( ) ;
125129 self . tail = [ ] . iter ( ) ;
130+ mem:: swap ( & mut self . head , & mut self . tail ) ;
126131 }
127- self . head . nth_back ( n)
132+ self . tail . nth_back ( n)
128133 }
129134
130135 fn rfold < Acc , F > ( self , mut acc : Acc , mut f : F ) -> Acc
@@ -196,7 +201,12 @@ impl<'a, K, V> Iterator for BucketsMut<'a, K, V> {
196201 fn next ( & mut self ) -> Option < Self :: Item > {
197202 match self . head . next ( ) {
198203 next @ Some ( _) => next,
199- None => self . tail . next ( ) ,
204+ None => {
205+ // Swap so the rest is found on the first branch next time.
206+ // (Like `VecDeque` does in its own iterators.)
207+ mem:: swap ( & mut self . head , & mut self . tail ) ;
208+ self . head . next ( )
209+ }
200210 }
201211 }
202212
@@ -210,14 +220,12 @@ impl<'a, K, V> Iterator for BucketsMut<'a, K, V> {
210220 }
211221
212222 fn nth ( & mut self , mut n : usize ) -> Option < Self :: Item > {
213- if n < self . head . len ( ) {
214- return self . head . nth ( n) ;
215- }
216- if self . head . len ( ) > 0 {
223+ if n >= self . head . len ( ) {
217224 n -= self . head . len ( ) ;
218225 self . head = [ ] . iter_mut ( ) ;
226+ mem:: swap ( & mut self . head , & mut self . tail ) ;
219227 }
220- self . tail . nth ( n)
228+ self . head . nth ( n)
221229 }
222230
223231 fn last ( mut self ) -> Option < Self :: Item > {
@@ -244,19 +252,22 @@ impl<K, V> DoubleEndedIterator for BucketsMut<'_, K, V> {
244252 fn next_back ( & mut self ) -> Option < Self :: Item > {
245253 match self . tail . next_back ( ) {
246254 next @ Some ( _) => next,
247- None => self . head . next_back ( ) ,
255+ None => {
256+ // Swap so the rest is found on the first branch next time.
257+ // (Like `VecDeque` does in its own iterators.)
258+ mem:: swap ( & mut self . head , & mut self . tail ) ;
259+ self . tail . next_back ( )
260+ }
248261 }
249262 }
250263
251264 fn nth_back ( & mut self , mut n : usize ) -> Option < Self :: Item > {
252- if n < self . tail . len ( ) {
253- return self . tail . nth_back ( n) ;
254- }
255- if self . tail . len ( ) > 0 {
265+ if n >= self . tail . len ( ) {
256266 n -= self . tail . len ( ) ;
257267 self . tail = [ ] . iter_mut ( ) ;
268+ mem:: swap ( & mut self . head , & mut self . tail ) ;
258269 }
259- self . head . nth_back ( n)
270+ self . tail . nth_back ( n)
260271 }
261272
262273 fn rfold < Acc , F > ( self , acc : Acc , mut f : F ) -> Acc
0 commit comments