@@ -73,6 +73,7 @@ use crate::str_utils::{
73
73
last_line_start_byte_idx, line_to_byte_idx, trim_line_break,
74
74
} ;
75
75
use crate :: tree:: { Count , Node , TextInfo } ;
76
+ use crate :: two_way_peekable:: { TwoWayIterator , TwoWayPeekable } ;
76
77
77
78
//==========================================================
78
79
@@ -211,16 +212,20 @@ impl<'a> Bytes<'a> {
211
212
self
212
213
}
213
214
215
+ /// Return peekable version of the iterator
216
+ #[ inline( always) ]
217
+ pub fn two_way_peekable ( self ) -> TwoWayPeekable < Self > {
218
+ // this is needed, so that users can call `.two_way_peekable()` without importing TwoWayIterator
219
+ TwoWayIterator :: two_way_peekable ( self )
220
+ }
221
+
214
222
/// Advances the iterator backwards and returns the previous value.
215
223
///
216
224
/// Runs in amortized O(1) time and worst-case O(log N) time.
217
225
#[ inline( always) ]
218
226
pub fn prev ( & mut self ) -> Option < u8 > {
219
- if !self . is_reversed {
220
- self . prev_impl ( )
221
- } else {
222
- self . next_impl ( )
223
- }
227
+ // this is needed, so that users can call `.prev()` without importing TwoWayIterator
228
+ TwoWayIterator :: prev ( self )
224
229
}
225
230
226
231
#[ inline]
@@ -298,6 +303,16 @@ impl<'a> Iterator for Bytes<'a> {
298
303
}
299
304
}
300
305
306
+ impl < ' a > TwoWayIterator for Bytes < ' a > {
307
+ fn prev ( & mut self ) -> Option < Self :: Item > {
308
+ if !self . is_reversed {
309
+ self . prev_impl ( )
310
+ } else {
311
+ self . next_impl ( )
312
+ }
313
+ }
314
+ }
315
+
301
316
impl < ' a > ExactSizeIterator for Bytes < ' a > { }
302
317
303
318
//==========================================================
@@ -441,16 +456,20 @@ impl<'a> Chars<'a> {
441
456
self
442
457
}
443
458
459
+ /// Return peekable version of the iterator
460
+ #[ inline( always) ]
461
+ pub fn two_way_peekable ( self ) -> TwoWayPeekable < Self > {
462
+ // this is needed, so that users can call `.two_way_peekable()` without importing TwoWayIterator
463
+ TwoWayIterator :: two_way_peekable ( self )
464
+ }
465
+
444
466
/// Advances the iterator backwards and returns the previous value.
445
467
///
446
468
/// Runs in amortized O(1) time and worst-case O(log N) time.
447
469
#[ inline( always) ]
448
470
pub fn prev ( & mut self ) -> Option < char > {
449
- if !self . is_reversed {
450
- self . prev_impl ( )
451
- } else {
452
- self . next_impl ( )
453
- }
471
+ // this is needed, so that users can call `.prev()` without importing TwoWayIterator
472
+ TwoWayIterator :: prev ( self )
454
473
}
455
474
456
475
#[ inline]
@@ -536,6 +555,20 @@ impl<'a> Iterator for Chars<'a> {
536
555
}
537
556
}
538
557
558
+ impl < ' a > TwoWayIterator for Chars < ' a > {
559
+ /// Advances the iterator backwards and returns the previous value.
560
+ ///
561
+ /// Runs in amortized O(1) time and worst-case O(log N) time.
562
+ #[ inline( always) ]
563
+ fn prev ( & mut self ) -> Option < Self :: Item > {
564
+ if !self . is_reversed {
565
+ self . prev_impl ( )
566
+ } else {
567
+ self . next_impl ( )
568
+ }
569
+ }
570
+ }
571
+
539
572
impl < ' a > ExactSizeIterator for Chars < ' a > { }
540
573
541
574
//==========================================================
@@ -751,17 +784,20 @@ impl<'a> Lines<'a> {
751
784
self
752
785
}
753
786
787
+ /// Return peekable version of the iterator
788
+ #[ inline( always) ]
789
+ pub fn two_way_peekable ( self ) -> TwoWayPeekable < Self > {
790
+ // this is needed, so that users can call `.two_way_peekable()` without importing TwoWayIterator
791
+ TwoWayIterator :: two_way_peekable ( self )
792
+ }
793
+
754
794
/// Advances the iterator backwards and returns the previous value.
755
795
///
756
- /// Runs in O(1) time with respect to rope length and O(N) time with
757
- /// respect to line length.
796
+ /// Runs in amortized O(1) time and worst-case O(log N) time.
758
797
#[ inline( always) ]
759
798
pub fn prev ( & mut self ) -> Option < RopeSlice < ' a > > {
760
- if self . is_reversed {
761
- self . next_impl ( )
762
- } else {
763
- self . prev_impl ( )
764
- }
799
+ // this is needed, so that users can call `.prev()` without importing TwoWayIterator
800
+ TwoWayIterator :: prev ( self )
765
801
}
766
802
767
803
fn prev_impl ( & mut self ) -> Option < RopeSlice < ' a > > {
@@ -1244,6 +1280,16 @@ impl<'a> Iterator for Lines<'a> {
1244
1280
}
1245
1281
}
1246
1282
1283
+ impl < ' a > TwoWayIterator for Lines < ' a > {
1284
+ fn prev ( & mut self ) -> Option < Self :: Item > {
1285
+ if !self . is_reversed {
1286
+ self . prev_impl ( )
1287
+ } else {
1288
+ self . next_impl ( )
1289
+ }
1290
+ }
1291
+ }
1292
+
1247
1293
impl ExactSizeIterator for Lines < ' _ > { }
1248
1294
1249
1295
//==========================================================
@@ -1535,16 +1581,20 @@ impl<'a> Chunks<'a> {
1535
1581
self
1536
1582
}
1537
1583
1584
+ /// Return peekable version of the iterator
1585
+ #[ inline( always) ]
1586
+ pub fn two_way_peekable ( self ) -> TwoWayPeekable < Self > {
1587
+ // this is needed, so that users can call `.two_way_peekable()` without importing TwoWayIterator
1588
+ TwoWayIterator :: two_way_peekable ( self )
1589
+ }
1590
+
1538
1591
/// Advances the iterator backwards and returns the previous value.
1539
1592
///
1540
1593
/// Runs in amortized O(1) time and worst-case O(log N) time.
1541
1594
#[ inline( always) ]
1542
1595
pub fn prev ( & mut self ) -> Option < & ' a str > {
1543
- if !self . is_reversed {
1544
- self . prev_impl ( )
1545
- } else {
1546
- self . next_impl ( )
1547
- }
1596
+ // this is needed, so that users can call `.prev()` without importing TwoWayIterator
1597
+ TwoWayIterator :: prev ( self )
1548
1598
}
1549
1599
1550
1600
fn prev_impl ( & mut self ) -> Option < & ' a str > {
@@ -1713,6 +1763,16 @@ impl<'a> Iterator for Chunks<'a> {
1713
1763
}
1714
1764
}
1715
1765
1766
+ impl < ' a > TwoWayIterator for Chunks < ' a > {
1767
+ fn prev ( & mut self ) -> Option < Self :: Item > {
1768
+ if !self . is_reversed {
1769
+ self . prev_impl ( )
1770
+ } else {
1771
+ self . next_impl ( )
1772
+ }
1773
+ }
1774
+ }
1775
+
1716
1776
#[ cfg( test) ]
1717
1777
mod tests {
1718
1778
#![ allow( clippy:: while_let_on_iterator) ]
0 commit comments