Skip to content

Commit f9ccc34

Browse files
committed
Add more FusedIterator
Mark FusedIterator to WhileSome, RcIter, PadUsing, TupleWindows, TupleCombinations, RepeatN, Powerset, CombinationsWithReplacement, WithPosition, KMergeBy
1 parent ccc5081 commit f9ccc34

10 files changed

+76
-3
lines changed

src/adaptors/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ impl<I, T> Iterator for TupleCombinations<I, T>
728728
}
729729
}
730730

731+
impl<I, T> FusedIterator for TupleCombinations<I, T>
732+
where I: FusedIterator,
733+
T: HasCombination<I>,
734+
{}
735+
731736
#[derive(Clone, Debug)]
732737
pub struct Tuple1Combination<I> {
733738
iter: I,

src/combinations_with_replacement.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use alloc::vec::Vec;
22
use std::fmt;
3+
use std::iter::FusedIterator;
34

45
use super::lazy_buffer::LazyBuffer;
56

@@ -100,3 +101,9 @@ where
100101
}
101102
}
102103
}
104+
105+
impl<I> FusedIterator for CombinationsWithReplacement<I>
106+
where
107+
I: Iterator,
108+
I::Item: Clone,
109+
{}

src/kmerge_impl.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::size_hint;
22
use crate::Itertools;
33

44
use alloc::vec::Vec;
5+
use std::iter::FusedIterator;
56
use std::mem::replace;
67
use std::fmt;
78

@@ -219,3 +220,8 @@ impl<I, F> Iterator for KMergeBy<I, F>
219220
.unwrap_or((0, Some(0)))
220221
}
221222
}
223+
224+
impl<I, F> FusedIterator for KMergeBy<I, F>
225+
where I: Iterator,
226+
F: KMergePredicate<I::Item>
227+
{}

src/pad_tail.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::iter::Fuse;
1+
use std::iter::{Fuse, FusedIterator};
22
use crate::size_hint;
33

44
/// An iterator adaptor that pads a sequence to a minimum length by filling
@@ -81,3 +81,9 @@ impl<I, F> ExactSizeIterator for PadUsing<I, F>
8181
where I: ExactSizeIterator,
8282
F: FnMut(usize) -> I::Item
8383
{}
84+
85+
86+
impl<I, F> FusedIterator for PadUsing<I, F>
87+
where I: FusedIterator,
88+
F: FnMut(usize) -> I::Item
89+
{}

src/powerset.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt;
2+
use std::iter::FusedIterator;
23
use std::usize;
34
use alloc::vec::Vec;
45

@@ -81,3 +82,9 @@ impl<I> Iterator for Powerset<I>
8182
}
8283
}
8384
}
85+
86+
impl<I> FusedIterator for Powerset<I>
87+
where
88+
I: Iterator,
89+
I::Item: Clone,
90+
{}

src/rciter_impl.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
use std::iter::IntoIterator;
2+
use std::iter::{FusedIterator, IntoIterator};
33
use alloc::rc::Rc;
44
use std::cell::RefCell;
55

@@ -93,3 +93,8 @@ impl<'a, I> IntoIterator for &'a RcIter<I>
9393
self.clone()
9494
}
9595
}
96+
97+
98+
impl<A, I> FusedIterator for RcIter<I>
99+
where I: FusedIterator<Item = A>
100+
{}

src/repeatn.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::iter::FusedIterator;
12

23
/// An iterator that produces *n* repetitions of an element.
34
///
@@ -52,3 +53,7 @@ impl<A> DoubleEndedIterator for RepeatN<A>
5253
impl<A> ExactSizeIterator for RepeatN<A>
5354
where A: Clone
5455
{}
56+
57+
impl<A> FusedIterator for RepeatN<A>
58+
where A: Clone
59+
{}

src/tuple_impl.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Some iterator that produces tuples
22
33
use std::iter::Fuse;
4+
use std::iter::FusedIterator;
45
use std::iter::Take;
56
use std::iter::Cycle;
67
use std::marker::PhantomData;
@@ -187,6 +188,12 @@ impl<I, T> Iterator for TupleWindows<I, T>
187188
}
188189
}
189190

191+
impl<I, T> FusedIterator for TupleWindows<I, T>
192+
where I: FusedIterator<Item = T::Item>,
193+
T: HomogeneousTuple + Clone,
194+
T::Item: Clone
195+
{}
196+
190197
/// An iterator over all windows,wrapping back to the first elements when the
191198
/// window would otherwise exceed the length of the iterator, producing tuples
192199
/// of a specific size.

src/with_position.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::iter::{Fuse,Peekable};
1+
use std::iter::{Fuse,Peekable, FusedIterator};
22

33
/// An iterator adaptor that wraps each element in an [`Position`].
44
///
@@ -95,3 +95,6 @@ impl<I: Iterator> Iterator for WithPosition<I> {
9595
impl<I> ExactSizeIterator for WithPosition<I>
9696
where I: ExactSizeIterator,
9797
{ }
98+
99+
impl<I: Iterator> FusedIterator for WithPosition<I>
100+
{}

tests/quick.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,18 @@ quickcheck! {
16181618
is_fused(a.combinations(3))
16191619
}
16201620

1621+
fn fused_combination_with_replacement(a: Iter<i16>) -> bool
1622+
{
1623+
is_fused(a.clone().combinations_with_replacement(1)) &&
1624+
is_fused(a.combinations_with_replacement(3))
1625+
}
1626+
1627+
fn fused_tuple_combination(a: Iter<i16>) -> bool
1628+
{
1629+
is_fused(a.clone().fuse().tuple_combinations::<(_,)>()) &&
1630+
is_fused(a.fuse().tuple_combinations::<(_,_,_)>())
1631+
}
1632+
16211633
fn fused_unique(a: Iter<i16>) -> bool
16221634
{
16231635
is_fused(a.fuse().unique())
@@ -1669,5 +1681,15 @@ quickcheck! {
16691681
!is_fused(a.clone().update(|x|*x+=1)) &&
16701682
is_fused(a.fuse().update(|x|*x+=1))
16711683
}
1684+
1685+
fn fused_tuple_windows(a: Iter<i16>) -> bool
1686+
{
1687+
is_fused(a.fuse().tuple_windows::<(_,_)>())
1688+
}
1689+
1690+
fn fused_pad_using(a: Iter<i16>) -> bool
1691+
{
1692+
is_fused(a.fuse().pad_using(100,|_|0))
1693+
}
16721694
}
16731695

0 commit comments

Comments
 (0)