diff --git a/src/libcore/iterator.rs b/src/libcore/iterator.rs index 8bbf843085809..5e95485b27360 100644 --- a/src/libcore/iterator.rs +++ b/src/libcore/iterator.rs @@ -29,7 +29,7 @@ pub trait Iterator { /// /// In the future these will be default methods instead of a utility trait. pub trait IteratorUtil { - fn chain(self, other: Self) -> ChainIterator; + fn chain>(self, other: U) -> ChainIterator; fn zip>(self, other: U) -> ZipIterator; // 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 { /// In the future these will be default methods instead of a utility trait. impl> IteratorUtil for T { #[inline(always)] - fn chain(self, other: T) -> ChainIterator { + fn chain>(self, other: U) -> ChainIterator { ChainIterator{a: self, b: other, flag: false} } @@ -115,13 +115,13 @@ impl> IteratorUtil for T { } } -pub struct ChainIterator { +pub struct ChainIterator { priv a: T, - priv b: T, + priv b: U, priv flag: bool } -impl> Iterator for ChainIterator { +impl, U: Iterator> Iterator for ChainIterator { #[inline] fn next(&mut self) -> Option { 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]