@@ -127,7 +127,7 @@ pub mod structs {
127
127
pub use crate :: sources:: { RepeatCall , Unfold , Iterate } ;
128
128
#[ cfg( feature = "use_std" ) ]
129
129
pub use crate :: tee:: Tee ;
130
- pub use crate :: tuple_impl:: { TupleBuffer , TupleWindows , Tuples } ;
130
+ pub use crate :: tuple_impl:: { TupleBuffer , TupleWindows , CircularTupleWindows , Tuples } ;
131
131
#[ cfg( feature = "use_std" ) ]
132
132
pub use crate :: unique_impl:: { Unique , UniqueBy } ;
133
133
pub use crate :: with_position:: WithPosition ;
@@ -584,6 +584,40 @@ pub trait Itertools : Iterator {
584
584
tuple_impl:: tuple_windows ( self )
585
585
}
586
586
587
+ /// Return an iterator over all windows, wrapping back to the first
588
+ /// elements when the window would otherwise exceed the length of the
589
+ /// iterator, producing tuples of a specific size (up to 4).
590
+ ///
591
+ /// `circular_tuple_windows` clones the iterator elements so that they can be
592
+ /// part of successive windows, this makes it most suited for iterators
593
+ /// of references and other values that are cheap to copy.
594
+ ///
595
+ /// ```
596
+ /// use itertools::Itertools;
597
+ /// let mut v = Vec::new();
598
+ /// for (a, b) in (1..5).circular_tuple_windows() {
599
+ /// v.push((a, b));
600
+ /// }
601
+ /// assert_eq!(v, vec![(1, 2), (2, 3), (3, 4), (4, 1)]);
602
+ ///
603
+ /// let mut it = (1..5).circular_tuple_windows();
604
+ /// assert_eq!(Some((1, 2, 3)), it.next());
605
+ /// assert_eq!(Some((2, 3, 4)), it.next());
606
+ /// assert_eq!(Some((3, 4, 1)), it.next());
607
+ /// assert_eq!(Some((4, 1, 2)), it.next());
608
+ /// assert_eq!(None, it.next());
609
+ ///
610
+ /// // this requires a type hint
611
+ /// let it = (1..5).circular_tuple_windows::<(_, _, _)>();
612
+ /// itertools::assert_equal(it, vec![(1, 2, 3), (2, 3, 4), (3, 4, 1), (4, 1, 2)]);
613
+ /// ```
614
+ fn circular_tuple_windows < T > ( self ) -> CircularTupleWindows < Self , T >
615
+ where Self : Sized + Clone + Iterator < Item = T :: Item > + ExactSizeIterator ,
616
+ T : tuple_impl:: TupleCollect + Clone ,
617
+ T :: Item : Clone
618
+ {
619
+ tuple_impl:: circular_tuple_windows ( self )
620
+ }
587
621
/// Return an iterator that groups the items in tuples of a specific size
588
622
/// (up to 4).
589
623
///
0 commit comments