Skip to content

Commit

Permalink
auto merge of #6177 : gifnksm/rust/iter-chain, r=thestinger
Browse files Browse the repository at this point in the history
`T: Iterator<A>` and `U: Iterator<A>` should be able to `chain` whether `T` and `U` are same or not.
  • Loading branch information
bors committed May 2, 2013
2 parents d1f7220 + 8a28970 commit 326d966
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/libcore/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub trait Iterator<A> {
///
/// In the future these will be default methods instead of a utility trait.
pub trait IteratorUtil<A> {
fn chain(self, other: Self) -> ChainIterator<Self>;
fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<Self, U>;
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<Self, U>;
// FIXME: #5898: should be called map
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>;
Expand All @@ -50,7 +50,7 @@ pub trait IteratorUtil<A> {
/// In the future these will be default methods instead of a utility trait.
impl<A, T: Iterator<A>> IteratorUtil<A> for T {
#[inline(always)]
fn chain(self, other: T) -> ChainIterator<T> {
fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<T, U> {
ChainIterator{a: self, b: other, flag: false}
}

Expand Down Expand Up @@ -115,13 +115,13 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
}
}

pub struct ChainIterator<T> {
pub struct ChainIterator<T, U> {
priv a: T,
priv b: T,
priv b: U,
priv flag: bool
}

impl<A, T: Iterator<A>> Iterator<A> for ChainIterator<T> {
impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<T, U> {
#[inline]
fn next(&mut self) -> Option<A> {
if self.flag {
Expand Down Expand Up @@ -385,7 +385,7 @@ mod tests {
#[test]
fn test_iterator_chain() {
let xs = [0u, 1, 2, 3, 4, 5];
let ys = [30, 40, 50, 60];
let ys = [30u, 40, 50, 60];
let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60];
let mut it = xs.iter().chain(ys.iter());
let mut i = 0;
Expand All @@ -394,6 +394,15 @@ mod tests {
i += 1;
}
assert_eq!(i, expected.len());

let ys = Counter::new(30u, 10).take(4);
let mut it = xs.iter().transform(|&x| x).chain(ys);
let mut i = 0;
for it.advance |x: uint| {
assert_eq!(x, expected[i]);
i += 1;
}
assert_eq!(i, expected.len());
}

#[test]
Expand Down

0 comments on commit 326d966

Please sign in to comment.