auto merge of #6177 : gifnksm/rust/iter-chain, r=thestinger

`T: Iterator<A>` and `U: Iterator<A>` should be able to `chain` whether `T` and `U` are same or not.
This commit is contained in:
bors 2013-05-02 06:15:37 -07:00
commit 326d9661b7

View File

@ -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>;
@ -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}
}
@ -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 {
@ -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;
@ -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]