diff --git a/src/libcore/char/mod.rs b/src/libcore/char/mod.rs index 15e153bdfada2..95b7c066793fc 100644 --- a/src/libcore/char/mod.rs +++ b/src/libcore/char/mod.rs @@ -389,11 +389,17 @@ 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 {} +#[stable(feature = "exact_size_case_mapping_iter", since = "1.34.0")] +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 @@ -411,11 +417,17 @@ 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 {} +#[stable(feature = "exact_size_case_mapping_iter", since = "1.34.0")] +impl ExactSizeIterator for ToUppercase {} + #[derive(Debug, Clone)] enum CaseMappingIter { Three(char, char, char), @@ -457,6 +469,16 @@ impl Iterator for CaseMappingIter { CaseMappingIter::Zero => None, } } + + fn size_hint(&self) -> (usize, Option) { + let size = match self { + CaseMappingIter::Three(..) => 3, + CaseMappingIter::Two(..) => 2, + CaseMappingIter::One(_) => 1, + CaseMappingIter::Zero => 0, + }; + (size, Some(size)) + } } impl fmt::Display for CaseMappingIter { diff --git a/src/libcore/tests/char.rs b/src/libcore/tests/char.rs index 61856242c5706..579feed288adb 100644 --- a/src/libcore/tests/char.rs +++ b/src/libcore/tests/char.rs @@ -76,6 +76,8 @@ fn test_to_digit() { #[test] fn test_to_lowercase() { fn lower(c: char) -> String { + let to_lowercase = c.to_uppercase(); + assert_eq!(to_lowercase.len(), to_lowercase.count()); let iter: String = c.to_lowercase().collect(); let disp: String = c.to_lowercase().to_string(); assert_eq!(iter, disp); @@ -101,6 +103,8 @@ fn test_to_lowercase() { #[test] fn test_to_uppercase() { fn upper(c: char) -> String { + let to_uppercase = c.to_uppercase(); + assert_eq!(to_uppercase.len(), to_uppercase.count()); let iter: String = c.to_uppercase().collect(); let disp: String = c.to_uppercase().to_string(); assert_eq!(iter, disp);