Skip to content

Commit 1119de5

Browse files
committed
Add HomogeneousTuple trait.
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.
1 parent 775274c commit 1119de5

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(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

@@ -64,7 +76,7 @@ impl<T> ExactSizeIterator for TupleBuffer<T>
6476
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
6577
pub struct Tuples<I, T>
6678
where I: Iterator<Item = T::Item>,
67-
T: TupleCollect
79+
T: HomogeneousTuple
6880
{
6981
iter: Fuse<I>,
7082
buf: T::Buffer,
@@ -73,7 +85,7 @@ pub struct Tuples<I, T>
7385
/// Create a new tuples iterator.
7486
pub fn tuples<I, T>(iter: I) -> Tuples<I, T>
7587
where I: Iterator<Item = T::Item>,
76-
T: TupleCollect
88+
T: HomogeneousTuple
7789
{
7890
Tuples {
7991
iter: iter.fuse(),
@@ -83,7 +95,7 @@ pub fn tuples<I, T>(iter: I) -> Tuples<I, T>
8395

8496
impl<I, T> Iterator for Tuples<I, T>
8597
where I: Iterator<Item = T::Item>,
86-
T: TupleCollect
98+
T: HomogeneousTuple
8799
{
88100
type Item = T;
89101

@@ -94,7 +106,7 @@ impl<I, T> Iterator for Tuples<I, T>
94106

95107
impl<I, T> Tuples<I, T>
96108
where I: Iterator<Item = T::Item>,
97-
T: TupleCollect
109+
T: HomogeneousTuple
98110
{
99111
/// Return a buffer with the produced items that was not enough to be grouped in a tuple.
100112
///
@@ -120,7 +132,7 @@ impl<I, T> Tuples<I, T>
120132
#[derive(Debug)]
121133
pub struct TupleWindows<I, T>
122134
where I: Iterator<Item = T::Item>,
123-
T: TupleCollect
135+
T: HomogeneousTuple
124136
{
125137
iter: I,
126138
last: Option<T>,
@@ -129,7 +141,7 @@ pub struct TupleWindows<I, T>
129141
/// Create a new tuple windows iterator.
130142
pub fn tuple_windows<I, T>(mut iter: I) -> TupleWindows<I, T>
131143
where I: Iterator<Item = T::Item>,
132-
T: TupleCollect,
144+
T: HomogeneousTuple,
133145
T::Item: Clone
134146
{
135147
use std::iter::once;
@@ -152,7 +164,7 @@ pub fn tuple_windows<I, T>(mut iter: I) -> TupleWindows<I, T>
152164

153165
impl<I, T> Iterator for TupleWindows<I, T>
154166
where I: Iterator<Item = T::Item>,
155-
T: TupleCollect + Clone,
167+
T: HomogeneousTuple + Clone,
156168
T::Item: Clone
157169
{
158170
type Item = T;

0 commit comments

Comments
 (0)