diff --git a/src/libcore/char/mod.rs b/src/libcore/char/mod.rs index 4f6c302247dd2..7c7ed2877f537 100644 --- a/src/libcore/char/mod.rs +++ b/src/libcore/char/mod.rs @@ -404,11 +404,16 @@ impl Iterator for ToLowercase { fn next(&mut self) -> Option { self.0.next() } + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() + } } #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for ToLowercase {} +impl ExactSizeIterator for ToLowercase {} + /// Returns an iterator that yields the uppercase equivalent of a `char`. /// /// This `struct` is created by the [`to_uppercase`] method on [`char`]. See @@ -426,11 +431,16 @@ impl Iterator for ToUppercase { fn next(&mut self) -> Option { self.0.next() } + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() + } } #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for ToUppercase {} +impl ExactSizeIterator for ToUppercase {} + #[derive(Debug, Clone)] enum CaseMappingIter { Three(char, char, char), @@ -472,8 +482,26 @@ impl Iterator for CaseMappingIter { CaseMappingIter::Zero => None, } } + fn size_hint(&self) -> (usize, Option) { + match *self { + CaseMappingIter::Three(_, _, _) => { + (3, Some(3)) + } + CaseMappingIter::Two(_, _) => { + (2, Some(2)) + } + CaseMappingIter::One(_) => { + (1, Some(1)) + } + CaseMappingIter::Zero => (0, Some(0)) + } + } } +impl FusedIterator for CaseMappingIter {} + +impl ExactSizeIterator for CaseMappingIter {} + impl fmt::Display for CaseMappingIter { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { diff --git a/src/libcore/str/lossy.rs b/src/libcore/str/lossy.rs index 30b7267da7c5e..01e1c3eaaae01 100644 --- a/src/libcore/str/lossy.rs +++ b/src/libcore/str/lossy.rs @@ -13,6 +13,7 @@ use str as core_str; use fmt; use fmt::Write; use mem; +use iter::FusedIterator; /// Lossy UTF-8 string. #[unstable(feature = "str_internals", issue = "0")] @@ -146,7 +147,13 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> { broken: &[], }; self.source = &[]; - return Some(r); + Some(r) + } + + fn size_hint(&self) -> (usize, Option) { + // We might return no characters (we encounter an error). + // We will return the most characters when the input is all ASCII. + (0, Some(self.source.len())) } } @@ -177,6 +184,8 @@ impl fmt::Display for Utf8Lossy { } } +impl<'a> FusedIterator for Utf8LossyChunksIter<'a> {} + impl fmt::Debug for Utf8Lossy { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_char('"')?; diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index b39d9feb35b7e..aa111fd42434a 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -4316,6 +4316,10 @@ impl<'a> Iterator for SplitWhitespace<'a> { fn next(&mut self) -> Option<&'a str> { self.inner.next() } + + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } } #[stable(feature = "split_whitespace", since = "1.1.0")]