Skip to content

Commit 326d966

Browse files
committed
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.
2 parents d1f7220 + 8a28970 commit 326d966

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/libcore/iterator.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub trait Iterator<A> {
2929
///
3030
/// In the future these will be default methods instead of a utility trait.
3131
pub trait IteratorUtil<A> {
32-
fn chain(self, other: Self) -> ChainIterator<Self>;
32+
fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<Self, U>;
3333
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<Self, U>;
3434
// FIXME: #5898: should be called map
3535
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>;
@@ -50,7 +50,7 @@ pub trait IteratorUtil<A> {
5050
/// In the future these will be default methods instead of a utility trait.
5151
impl<A, T: Iterator<A>> IteratorUtil<A> for T {
5252
#[inline(always)]
53-
fn chain(self, other: T) -> ChainIterator<T> {
53+
fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<T, U> {
5454
ChainIterator{a: self, b: other, flag: false}
5555
}
5656

@@ -115,13 +115,13 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
115115
}
116116
}
117117

118-
pub struct ChainIterator<T> {
118+
pub struct ChainIterator<T, U> {
119119
priv a: T,
120-
priv b: T,
120+
priv b: U,
121121
priv flag: bool
122122
}
123123

124-
impl<A, T: Iterator<A>> Iterator<A> for ChainIterator<T> {
124+
impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<T, U> {
125125
#[inline]
126126
fn next(&mut self) -> Option<A> {
127127
if self.flag {
@@ -385,7 +385,7 @@ mod tests {
385385
#[test]
386386
fn test_iterator_chain() {
387387
let xs = [0u, 1, 2, 3, 4, 5];
388-
let ys = [30, 40, 50, 60];
388+
let ys = [30u, 40, 50, 60];
389389
let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60];
390390
let mut it = xs.iter().chain(ys.iter());
391391
let mut i = 0;
@@ -394,6 +394,15 @@ mod tests {
394394
i += 1;
395395
}
396396
assert_eq!(i, expected.len());
397+
398+
let ys = Counter::new(30u, 10).take(4);
399+
let mut it = xs.iter().transform(|&x| x).chain(ys);
400+
let mut i = 0;
401+
for it.advance |x: uint| {
402+
assert_eq!(x, expected[i]);
403+
i += 1;
404+
}
405+
assert_eq!(i, expected.len());
397406
}
398407

399408
#[test]

0 commit comments

Comments
 (0)