Skip to content

Commit adb66f1

Browse files
bors[bot]jswrenn
andauthored
Merge #389
389: Add `HomogeneousTuple` trait. r=jswrenn a=jswrenn Allows clients to use certain tuple methods of `Itertools` in more generic contexts. This trait serves as a public facade for `TupleCollect`, an internal implementation detail. Fixes #387. Co-authored-by: Jack Wrenn <[email protected]>
2 parents afcdd69 + 1119de5 commit adb66f1

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ pub mod structs {
137137
pub use zip_longest::ZipLongest;
138138
pub use ziptuple::Zip;
139139
}
140+
141+
/// Traits helpful for using certain `Itertools` methods in generic contexts.
142+
pub mod traits {
143+
pub use ::tuple_impl::HomogeneousTuple;
144+
}
145+
140146
#[allow(deprecated)]
141147
pub use structs::*;
142148
pub use concat_impl::concat;
@@ -588,7 +594,7 @@ pub trait Itertools : Iterator {
588594
/// ```
589595
fn tuple_windows<T>(self) -> TupleWindows<Self, T>
590596
where Self: Sized + Iterator<Item = T::Item>,
591-
T: tuple_impl::TupleCollect,
597+
T: traits::HomogeneousTuple,
592598
T::Item: Clone
593599
{
594600
tuple_impl::tuple_windows(self)
@@ -627,7 +633,7 @@ pub trait Itertools : Iterator {
627633
/// See also [`Tuples::into_buffer`](structs/struct.Tuples.html#method.into_buffer).
628634
fn tuples<T>(self) -> Tuples<Self, T>
629635
where Self: Sized + Iterator<Item = T::Item>,
630-
T: tuple_impl::TupleCollect
636+
T: traits::HomogeneousTuple
631637
{
632638
tuple_impl::tuples(self)
633639
}
@@ -1352,7 +1358,7 @@ pub trait Itertools : Iterator {
13521358
/// ```
13531359
fn next_tuple<T>(&mut self) -> Option<T>
13541360
where Self: Sized + Iterator<Item = T::Item>,
1355-
T: tuple_impl::TupleCollect
1361+
T: traits::HomogeneousTuple
13561362
{
13571363
T::collect_from_iter_no_buf(self)
13581364
}
@@ -1377,7 +1383,7 @@ pub trait Itertools : Iterator {
13771383
/// ```
13781384
fn collect_tuple<T>(mut self) -> Option<T>
13791385
where Self: Sized + Iterator<Item = T::Item>,
1380-
T: tuple_impl::TupleCollect
1386+
T: traits::HomogeneousTuple
13811387
{
13821388
match self.next_tuple() {
13831389
elt @ Some(_) => match self.next() {

src/tuple_impl.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@
22
33
use std::iter::Fuse;
44

5+
// `HomogeneousTuple` is a public facade for `TupleCollect`, allowing
6+
// tuple-related methods to be used by clients in generic contexts, while
7+
// hiding the implementation details of `TupleCollect`.
8+
// See https://github.com/rust-itertools/itertools/issues/387
9+
10+
/// Implemented for homogeneous tuples of size up to 4.
11+
pub trait HomogeneousTuple
12+
: TupleCollect
13+
{}
14+
15+
impl<T: TupleCollect> HomogeneousTuple for T {}
16+
517
/// An iterator over a incomplete tuple.
618
///
719
/// See [`.tuples()`](../trait.Itertools.html#method.tuples) and
820
/// [`Tuples::into_buffer()`](struct.Tuples.html#method.into_buffer).
921
#[derive(Clone, Debug)]
1022
pub struct TupleBuffer<T>
11-
where T: TupleCollect
23+
where T: HomogeneousTuple
1224
{
1325
cur: usize,
1426
buf: T::Buffer,
1527
}
1628

1729
impl<T> TupleBuffer<T>
18-
where T: TupleCollect
30+
where T: HomogeneousTuple
1931
{
2032
fn new(buf: T::Buffer) -> Self {
2133
TupleBuffer {
@@ -26,7 +38,7 @@ impl<T> TupleBuffer<T>
2638
}
2739

2840
impl<T> Iterator for TupleBuffer<T>
29-
where T: TupleCollect
41+
where T: HomogeneousTuple
3042
{
3143
type Item = T::Item;
3244

@@ -54,7 +66,7 @@ impl<T> Iterator for TupleBuffer<T>
5466
}
5567

5668
impl<T> ExactSizeIterator for TupleBuffer<T>
57-
where T: TupleCollect
69+
where T: HomogeneousTuple
5870
{
5971
}
6072

@@ -65,7 +77,7 @@ impl<T> ExactSizeIterator for TupleBuffer<T>
6577
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
6678
pub struct Tuples<I, T>
6779
where I: Iterator<Item = T::Item>,
68-
T: TupleCollect
80+
T: HomogeneousTuple
6981
{
7082
iter: Fuse<I>,
7183
buf: T::Buffer,
@@ -74,7 +86,7 @@ pub struct Tuples<I, T>
7486
/// Create a new tuples iterator.
7587
pub fn tuples<I, T>(iter: I) -> Tuples<I, T>
7688
where I: Iterator<Item = T::Item>,
77-
T: TupleCollect
89+
T: HomogeneousTuple
7890
{
7991
Tuples {
8092
iter: iter.fuse(),
@@ -84,7 +96,7 @@ pub fn tuples<I, T>(iter: I) -> Tuples<I, T>
8496

8597
impl<I, T> Iterator for Tuples<I, T>
8698
where I: Iterator<Item = T::Item>,
87-
T: TupleCollect
99+
T: HomogeneousTuple
88100
{
89101
type Item = T;
90102

@@ -95,7 +107,7 @@ impl<I, T> Iterator for Tuples<I, T>
95107

96108
impl<I, T> Tuples<I, T>
97109
where I: Iterator<Item = T::Item>,
98-
T: TupleCollect
110+
T: HomogeneousTuple
99111
{
100112
/// Return a buffer with the produced items that was not enough to be grouped in a tuple.
101113
///
@@ -121,7 +133,7 @@ impl<I, T> Tuples<I, T>
121133
#[derive(Clone, Debug)]
122134
pub struct TupleWindows<I, T>
123135
where I: Iterator<Item = T::Item>,
124-
T: TupleCollect
136+
T: HomogeneousTuple
125137
{
126138
iter: I,
127139
last: Option<T>,
@@ -130,7 +142,7 @@ pub struct TupleWindows<I, T>
130142
/// Create a new tuple windows iterator.
131143
pub fn tuple_windows<I, T>(mut iter: I) -> TupleWindows<I, T>
132144
where I: Iterator<Item = T::Item>,
133-
T: TupleCollect,
145+
T: HomogeneousTuple,
134146
T::Item: Clone
135147
{
136148
use std::iter::once;
@@ -153,7 +165,7 @@ pub fn tuple_windows<I, T>(mut iter: I) -> TupleWindows<I, T>
153165

154166
impl<I, T> Iterator for TupleWindows<I, T>
155167
where I: Iterator<Item = T::Item>,
156-
T: TupleCollect + Clone,
168+
T: HomogeneousTuple + Clone,
157169
T::Item: Clone
158170
{
159171
type Item = T;

0 commit comments

Comments
 (0)