Skip to content

Commit

Permalink
Add more FusedIterator
Browse files Browse the repository at this point in the history
Mark FusedIterator to WhileSome, RcIter, PadUsing, TupleWindows,
TupleCombinations, RepeatN, Powerset, CombinationsWithReplacement,
WithPosition, KMergeBy
  • Loading branch information
aobatact committed May 26, 2021
1 parent ccc5081 commit f9ccc34
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,11 @@ impl<I, T> Iterator for TupleCombinations<I, T>
}
}

impl<I, T> FusedIterator for TupleCombinations<I, T>
where I: FusedIterator,
T: HasCombination<I>,
{}

#[derive(Clone, Debug)]
pub struct Tuple1Combination<I> {
iter: I,
Expand Down
7 changes: 7 additions & 0 deletions src/combinations_with_replacement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::vec::Vec;
use std::fmt;
use std::iter::FusedIterator;

use super::lazy_buffer::LazyBuffer;

Expand Down Expand Up @@ -100,3 +101,9 @@ where
}
}
}

impl<I> FusedIterator for CombinationsWithReplacement<I>
where
I: Iterator,
I::Item: Clone,
{}
6 changes: 6 additions & 0 deletions src/kmerge_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::size_hint;
use crate::Itertools;

use alloc::vec::Vec;
use std::iter::FusedIterator;
use std::mem::replace;
use std::fmt;

Expand Down Expand Up @@ -219,3 +220,8 @@ impl<I, F> Iterator for KMergeBy<I, F>
.unwrap_or((0, Some(0)))
}
}

impl<I, F> FusedIterator for KMergeBy<I, F>
where I: Iterator,
F: KMergePredicate<I::Item>
{}
8 changes: 7 additions & 1 deletion src/pad_tail.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::iter::Fuse;
use std::iter::{Fuse, FusedIterator};
use crate::size_hint;

/// An iterator adaptor that pads a sequence to a minimum length by filling
Expand Down Expand Up @@ -81,3 +81,9 @@ impl<I, F> ExactSizeIterator for PadUsing<I, F>
where I: ExactSizeIterator,
F: FnMut(usize) -> I::Item
{}


impl<I, F> FusedIterator for PadUsing<I, F>
where I: FusedIterator,
F: FnMut(usize) -> I::Item
{}
7 changes: 7 additions & 0 deletions src/powerset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use std::iter::FusedIterator;
use std::usize;
use alloc::vec::Vec;

Expand Down Expand Up @@ -81,3 +82,9 @@ impl<I> Iterator for Powerset<I>
}
}
}

impl<I> FusedIterator for Powerset<I>
where
I: Iterator,
I::Item: Clone,
{}
7 changes: 6 additions & 1 deletion src/rciter_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::iter::IntoIterator;
use std::iter::{FusedIterator, IntoIterator};
use alloc::rc::Rc;
use std::cell::RefCell;

Expand Down Expand Up @@ -93,3 +93,8 @@ impl<'a, I> IntoIterator for &'a RcIter<I>
self.clone()
}
}


impl<A, I> FusedIterator for RcIter<I>
where I: FusedIterator<Item = A>
{}
5 changes: 5 additions & 0 deletions src/repeatn.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::iter::FusedIterator;

/// An iterator that produces *n* repetitions of an element.
///
Expand Down Expand Up @@ -52,3 +53,7 @@ impl<A> DoubleEndedIterator for RepeatN<A>
impl<A> ExactSizeIterator for RepeatN<A>
where A: Clone
{}

impl<A> FusedIterator for RepeatN<A>
where A: Clone
{}
7 changes: 7 additions & 0 deletions src/tuple_impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Some iterator that produces tuples
use std::iter::Fuse;
use std::iter::FusedIterator;
use std::iter::Take;
use std::iter::Cycle;
use std::marker::PhantomData;
Expand Down Expand Up @@ -187,6 +188,12 @@ impl<I, T> Iterator for TupleWindows<I, T>
}
}

impl<I, T> FusedIterator for TupleWindows<I, T>
where I: FusedIterator<Item = T::Item>,
T: HomogeneousTuple + Clone,
T::Item: Clone
{}

/// An iterator over all windows,wrapping back to the first elements when the
/// window would otherwise exceed the length of the iterator, producing tuples
/// of a specific size.
Expand Down
5 changes: 4 additions & 1 deletion src/with_position.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::iter::{Fuse,Peekable};
use std::iter::{Fuse,Peekable, FusedIterator};

/// An iterator adaptor that wraps each element in an [`Position`].
///
Expand Down Expand Up @@ -95,3 +95,6 @@ impl<I: Iterator> Iterator for WithPosition<I> {
impl<I> ExactSizeIterator for WithPosition<I>
where I: ExactSizeIterator,
{ }

impl<I: Iterator> FusedIterator for WithPosition<I>
{}
22 changes: 22 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,18 @@ quickcheck! {
is_fused(a.combinations(3))
}

fn fused_combination_with_replacement(a: Iter<i16>) -> bool
{
is_fused(a.clone().combinations_with_replacement(1)) &&
is_fused(a.combinations_with_replacement(3))
}

fn fused_tuple_combination(a: Iter<i16>) -> bool
{
is_fused(a.clone().fuse().tuple_combinations::<(_,)>()) &&
is_fused(a.fuse().tuple_combinations::<(_,_,_)>())
}

fn fused_unique(a: Iter<i16>) -> bool
{
is_fused(a.fuse().unique())
Expand Down Expand Up @@ -1669,5 +1681,15 @@ quickcheck! {
!is_fused(a.clone().update(|x|*x+=1)) &&
is_fused(a.fuse().update(|x|*x+=1))
}

fn fused_tuple_windows(a: Iter<i16>) -> bool
{
is_fused(a.fuse().tuple_windows::<(_,_)>())
}

fn fused_pad_using(a: Iter<i16>) -> bool
{
is_fused(a.fuse().pad_using(100,|_|0))
}
}

0 comments on commit f9ccc34

Please sign in to comment.