Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libcore: Make ChainIterator take two different-typed Iterators. #6177

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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