@@ -183,7 +183,7 @@ where
183
183
{
184
184
#[ inline]
185
185
fn next_back ( & mut self ) -> Option < A :: Item > {
186
- and_then_or_clear ( & mut self . b , |b| b . next_back ( ) ) . or_else ( || self . a . as_mut ( ) ? . next_back ( ) )
186
+ SpecChainBack :: next_back ( self )
187
187
}
188
188
189
189
#[ inline]
@@ -324,6 +324,10 @@ trait SpecChain: Iterator {
324
324
fn next ( & mut self ) -> Option < Self :: Item > ;
325
325
}
326
326
327
+ trait SpecChainBack : DoubleEndedIterator {
328
+ fn next_back ( & mut self ) -> Option < Self :: Item > ;
329
+ }
330
+
327
331
impl < A , B > SpecChain for Chain < A , B >
328
332
where
329
333
A : Iterator ,
@@ -335,6 +339,17 @@ where
335
339
}
336
340
}
337
341
342
+ impl < A , B > SpecChainBack for Chain < A , B >
343
+ where
344
+ A : DoubleEndedIterator ,
345
+ B : DoubleEndedIterator < Item = A :: Item > ,
346
+ {
347
+ #[ inline]
348
+ default fn next_back ( & mut self ) -> Option < Self :: Item > {
349
+ and_then_or_clear ( & mut self . b , |b| b. next_back ( ) ) . or_else ( || self . a . as_mut ( ) ?. next_back ( ) )
350
+ }
351
+ }
352
+
338
353
impl < A , B > SpecChain for Chain < A , B >
339
354
where
340
355
A : Iterator ,
@@ -352,3 +367,21 @@ where
352
367
result
353
368
}
354
369
}
370
+
371
+ impl < A , B > SpecChainBack for Chain < A , B >
372
+ where
373
+ A : DoubleEndedIterator ,
374
+ B : DoubleEndedIterator < Item = A :: Item > ,
375
+ Self : SymmetricalModuloLifetimes ,
376
+ {
377
+ #[ inline]
378
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
379
+ let mut result = and_then_or_clear ( & mut self . b , DoubleEndedIterator :: next_back) ;
380
+ if result. is_none ( ) {
381
+ // SAFETY: SymmetricalModuloLifetimes guarantees that A and B are safe to swap
382
+ unsafe { mem:: swap ( & mut self . a , & mut * ( & mut self . b as * mut _ as * mut Option < A > ) ) } ;
383
+ result = and_then_or_clear ( & mut self . b , DoubleEndedIterator :: next_back) ;
384
+ }
385
+ result
386
+ }
387
+ }
0 commit comments