diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 16e652fcc9a..32225b90f6b 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -1279,14 +1279,14 @@ pub struct Cloned { } #[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Cloned where - T: Clone, - D: Deref, - I: Iterator, +impl Iterator for Cloned where + I: Iterator, + I::Item: Deref, + ::Target: Clone { - type Item = T; + type Item = ::Target; - fn next(&mut self) -> Option { + fn next(&mut self) -> Option<::Item> { self.it.next().cloned() } @@ -1296,28 +1296,28 @@ impl Iterator for Cloned where } #[stable(feature = "rust1", since = "1.0.0")] -impl DoubleEndedIterator for Cloned where - T: Clone, - D: Deref, - I: DoubleEndedIterator, +impl DoubleEndedIterator for Cloned where + I: DoubleEndedIterator, + I::Item: Deref, + ::Target: Clone { - fn next_back(&mut self) -> Option { + fn next_back(&mut self) -> Option<::Item> { self.it.next_back().cloned() } } #[stable(feature = "rust1", since = "1.0.0")] -impl ExactSizeIterator for Cloned where - T: Clone, - D: Deref, - I: ExactSizeIterator, +impl ExactSizeIterator for Cloned where + I: ExactSizeIterator, + I::Item: Deref, + ::Target: Clone {} #[unstable(feature = "core", reason = "trait is experimental")] -impl RandomAccessIterator for Cloned where - T: Clone, - D: Deref, - I: RandomAccessIterator +impl RandomAccessIterator for Cloned where + I: RandomAccessIterator, + I::Item: Deref, + ::Target: Clone { #[inline] fn indexable(&self) -> usize { @@ -1325,7 +1325,7 @@ impl RandomAccessIterator for Cloned where } #[inline] - fn idx(&mut self, index: usize) -> Option { + fn idx(&mut self, index: usize) -> Option<::Item> { self.it.idx(index).cloned() } } @@ -1400,11 +1400,14 @@ pub struct Chain { } #[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Chain where A: Iterator, B: Iterator { - type Item = T; +impl Iterator for Chain where + A: Iterator, + B: Iterator +{ + type Item = A::Item; #[inline] - fn next(&mut self) -> Option { + fn next(&mut self) -> Option { if self.flag { self.b.next() } else { @@ -1434,12 +1437,12 @@ impl Iterator for Chain where A: Iterator, B: Iterator DoubleEndedIterator for Chain where - A: DoubleEndedIterator, - B: DoubleEndedIterator, +impl DoubleEndedIterator for Chain where + A: DoubleEndedIterator, + B: DoubleEndedIterator, { #[inline] - fn next_back(&mut self) -> Option { + fn next_back(&mut self) -> Option { match self.b.next_back() { Some(x) => Some(x), None => self.a.next_back() @@ -1448,9 +1451,9 @@ impl DoubleEndedIterator for Chain where } #[unstable(feature = "core", reason = "trait is experimental")] -impl RandomAccessIterator for Chain where - A: RandomAccessIterator, - B: RandomAccessIterator, +impl RandomAccessIterator for Chain where + A: RandomAccessIterator, + B: RandomAccessIterator, { #[inline] fn indexable(&self) -> usize { @@ -1459,7 +1462,7 @@ impl RandomAccessIterator for Chain where } #[inline] - fn idx(&mut self, index: usize) -> Option { + fn idx(&mut self, index: usize) -> Option { let len = self.a.indexable(); if index < len { self.a.idx(index) @@ -1479,14 +1482,12 @@ pub struct Zip { } #[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Zip where - A: Iterator, - B: Iterator, +impl Iterator for Zip where A: Iterator, B: Iterator { - type Item = (T, U); + type Item = (A::Item, B::Item); #[inline] - fn next(&mut self) -> Option<(T, U)> { + fn next(&mut self) -> Option<(A::Item, B::Item)> { match self.a.next() { None => None, Some(x) => match self.b.next() { @@ -1515,12 +1516,12 @@ impl Iterator for Zip where } #[stable(feature = "rust1", since = "1.0.0")] -impl DoubleEndedIterator for Zip where - A: DoubleEndedIterator + ExactSizeIterator, - B: DoubleEndedIterator + ExactSizeIterator, +impl DoubleEndedIterator for Zip where + A: DoubleEndedIterator + ExactSizeIterator, + B: DoubleEndedIterator + ExactSizeIterator, { #[inline] - fn next_back(&mut self) -> Option<(T, U)> { + fn next_back(&mut self) -> Option<(A::Item, B::Item)> { let a_sz = self.a.len(); let b_sz = self.b.len(); if a_sz != b_sz { @@ -1540,9 +1541,9 @@ impl DoubleEndedIterator for Zip where } #[unstable(feature = "core", reason = "trait is experimental")] -impl RandomAccessIterator for Zip where - A: RandomAccessIterator, - B: RandomAccessIterator, +impl RandomAccessIterator for Zip where + A: RandomAccessIterator, + B: RandomAccessIterator { #[inline] fn indexable(&self) -> usize { @@ -1550,7 +1551,7 @@ impl RandomAccessIterator for Zip where } #[inline] - fn idx(&mut self, index: usize) -> Option<(T, U)> { + fn idx(&mut self, index: usize) -> Option<(A::Item, B::Item)> { match self.a.idx(index) { None => None, Some(x) => match self.b.idx(index) { @@ -2058,8 +2059,9 @@ pub struct Scan { } #[stable(feature = "rust1", since = "1.0.0")] -impl, St, F> Iterator for Scan where - F: FnMut(&mut St, A) -> Option, +impl Iterator for Scan where + I: Iterator, + F: FnMut(&mut St, I::Item) -> Option, { type Item = B; diff --git a/src/test/run-pass/iter-cloned-type-inference.rs b/src/test/run-pass/iter-cloned-type-inference.rs new file mode 100644 index 00000000000..6ce226bbeca --- /dev/null +++ b/src/test/run-pass/iter-cloned-type-inference.rs @@ -0,0 +1,25 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test to see that the element type of .cloned() can be inferred +// properly. Previously this would fail to deduce the type of `sum`. + +#![feature(core)] + +use std::iter::AdditiveIterator; + +fn square_sum(v: &[i64]) -> i64 { + let sum = v.iter().cloned().sum(); + sum * sum +} + +fn main() { + assert_eq!(36, square_sum(&[1,2,3])); +}