@@ -179,7 +179,7 @@ where
179
179
{
180
180
#[ inline]
181
181
fn next_back ( & mut self ) -> Option < A :: Item > {
182
- and_then_or_clear ( & mut self . b , |b| b . next_back ( ) ) . or_else ( || self . a . as_mut ( ) ? . next_back ( ) )
182
+ SpecChainBack :: next_back ( self )
183
183
}
184
184
185
185
#[ inline]
@@ -337,6 +337,10 @@ trait SpecChain: Iterator {
337
337
fn next ( & mut self ) -> Option < Self :: Item > ;
338
338
}
339
339
340
+ trait SpecChainBack : DoubleEndedIterator {
341
+ fn next_back ( & mut self ) -> Option < Self :: Item > ;
342
+ }
343
+
340
344
impl < A , B > SpecChain for Chain < A , B >
341
345
where
342
346
A : Iterator ,
@@ -348,6 +352,17 @@ where
348
352
}
349
353
}
350
354
355
+ impl < A , B > SpecChainBack for Chain < A , B >
356
+ where
357
+ A : DoubleEndedIterator ,
358
+ B : DoubleEndedIterator < Item = A :: Item > ,
359
+ {
360
+ #[ inline]
361
+ default fn next_back ( & mut self ) -> Option < Self :: Item > {
362
+ and_then_or_clear ( & mut self . b , |b| b. next_back ( ) ) . or_else ( || self . a . as_mut ( ) ?. next_back ( ) )
363
+ }
364
+ }
365
+
351
366
impl < A , B > SpecChain for Chain < A , B >
352
367
where
353
368
A : Iterator ,
@@ -365,3 +380,21 @@ where
365
380
result
366
381
}
367
382
}
383
+
384
+ impl < A , B > SpecChainBack for Chain < A , B >
385
+ where
386
+ A : DoubleEndedIterator ,
387
+ B : DoubleEndedIterator < Item = A :: Item > ,
388
+ Self : SymmetricalModuloLifetimes ,
389
+ {
390
+ #[ inline]
391
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
392
+ let mut result = and_then_or_clear ( & mut self . b , DoubleEndedIterator :: next_back) ;
393
+ if result. is_none ( ) {
394
+ // SAFETY: SymmetricalModuloLifetimes guarantees that A and B are safe to swap
395
+ unsafe { mem:: swap ( & mut self . a , & mut * ( & mut self . b as * mut _ as * mut Option < A > ) ) } ;
396
+ result = and_then_or_clear ( & mut self . b , DoubleEndedIterator :: next_back) ;
397
+ }
398
+ result
399
+ }
400
+ }
0 commit comments