Use len
instead of size_hint
where appropiate
This makes it clearer that we're not just looking for a lower bound but rather know that the iterator is an `ExactSizeIterator`.
This commit is contained in:
parent
4960f2f907
commit
8ff5c4394c
6 changed files with 15 additions and 16 deletions
|
@ -1658,7 +1658,7 @@ impl<T> Iterator for IntoIter<T> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn count(self) -> usize {
|
fn count(self) -> usize {
|
||||||
self.size_hint().0
|
self.len()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1384,7 +1384,7 @@ impl Debug for str {
|
||||||
for (i, c) in self.char_indices() {
|
for (i, c) in self.char_indices() {
|
||||||
let esc = c.escape_default();
|
let esc = c.escape_default();
|
||||||
// If char needs escaping, flush backlog so far and write, else skip
|
// If char needs escaping, flush backlog so far and write, else skip
|
||||||
if esc.size_hint() != (1, Some(1)) {
|
if esc.len() != 1 {
|
||||||
f.write_str(&self[from..i])?;
|
f.write_str(&self[from..i])?;
|
||||||
for c in esc {
|
for c in esc {
|
||||||
f.write_char(c)?;
|
f.write_char(c)?;
|
||||||
|
|
|
@ -835,7 +835,7 @@ macro_rules! iterator {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn count(self) -> usize {
|
fn count(self) -> usize {
|
||||||
self.size_hint().0
|
self.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -1444,7 +1444,7 @@ impl<'a, T> Iterator for Windows<'a, T> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn count(self) -> usize {
|
fn count(self) -> usize {
|
||||||
self.size_hint().0
|
self.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -1541,7 +1541,7 @@ impl<'a, T> Iterator for Chunks<'a, T> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn count(self) -> usize {
|
fn count(self) -> usize {
|
||||||
self.size_hint().0
|
self.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -1632,7 +1632,7 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn count(self) -> usize {
|
fn count(self) -> usize {
|
||||||
self.size_hint().0
|
self.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -433,7 +433,7 @@ impl<'a> Iterator for Chars<'a> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
let (len, _) = self.iter.size_hint();
|
let len = self.iter.len();
|
||||||
// `(len + 3)` can't overflow, because we know that the `slice::Iter`
|
// `(len + 3)` can't overflow, because we know that the `slice::Iter`
|
||||||
// belongs to a slice in memory which has a maximum length of
|
// belongs to a slice in memory which has a maximum length of
|
||||||
// `isize::MAX` (that's well below `usize::MAX`).
|
// `isize::MAX` (that's well below `usize::MAX`).
|
||||||
|
@ -480,12 +480,12 @@ impl<'a> Iterator for CharIndices<'a> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn next(&mut self) -> Option<(usize, char)> {
|
fn next(&mut self) -> Option<(usize, char)> {
|
||||||
let (pre_len, _) = self.iter.iter.size_hint();
|
let pre_len = self.iter.iter.len();
|
||||||
match self.iter.next() {
|
match self.iter.next() {
|
||||||
None => None,
|
None => None,
|
||||||
Some(ch) => {
|
Some(ch) => {
|
||||||
let index = self.front_offset;
|
let index = self.front_offset;
|
||||||
let (len, _) = self.iter.iter.size_hint();
|
let len = self.iter.iter.len();
|
||||||
self.front_offset += pre_len - len;
|
self.front_offset += pre_len - len;
|
||||||
Some((index, ch))
|
Some((index, ch))
|
||||||
}
|
}
|
||||||
|
@ -505,8 +505,7 @@ impl<'a> DoubleEndedIterator for CharIndices<'a> {
|
||||||
match self.iter.next_back() {
|
match self.iter.next_back() {
|
||||||
None => None,
|
None => None,
|
||||||
Some(ch) => {
|
Some(ch) => {
|
||||||
let (len, _) = self.iter.iter.size_hint();
|
let index = self.front_offset + self.iter.iter.len();
|
||||||
let index = self.front_offset + len;
|
|
||||||
Some((index, ch))
|
Some((index, ch))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -310,9 +310,9 @@ unsafe impl<'a, C: CharEq> Searcher<'a> for CharEqSearcher<'a, C> {
|
||||||
let s = &mut self.char_indices;
|
let s = &mut self.char_indices;
|
||||||
// Compare lengths of the internal byte slice iterator
|
// Compare lengths of the internal byte slice iterator
|
||||||
// to find length of current char
|
// to find length of current char
|
||||||
let (pre_len, _) = s.iter.iter.size_hint();
|
let pre_len = s.iter.iter.len();
|
||||||
if let Some((i, c)) = s.next() {
|
if let Some((i, c)) = s.next() {
|
||||||
let (len, _) = s.iter.iter.size_hint();
|
let len = s.iter.iter.len();
|
||||||
let char_len = pre_len - len;
|
let char_len = pre_len - len;
|
||||||
if self.char_eq.matches(c) {
|
if self.char_eq.matches(c) {
|
||||||
return SearchStep::Match(i, i + char_len);
|
return SearchStep::Match(i, i + char_len);
|
||||||
|
@ -330,9 +330,9 @@ unsafe impl<'a, C: CharEq> ReverseSearcher<'a> for CharEqSearcher<'a, C> {
|
||||||
let s = &mut self.char_indices;
|
let s = &mut self.char_indices;
|
||||||
// Compare lengths of the internal byte slice iterator
|
// Compare lengths of the internal byte slice iterator
|
||||||
// to find length of current char
|
// to find length of current char
|
||||||
let (pre_len, _) = s.iter.iter.size_hint();
|
let pre_len = s.iter.iter.len();
|
||||||
if let Some((i, c)) = s.next_back() {
|
if let Some((i, c)) = s.next_back() {
|
||||||
let (len, _) = s.iter.iter.size_hint();
|
let len = s.iter.iter.len();
|
||||||
let char_len = pre_len - len;
|
let char_len = pre_len - len;
|
||||||
if self.char_eq.matches(c) {
|
if self.char_eq.matches(c) {
|
||||||
return SearchStep::Match(i, i + char_len);
|
return SearchStep::Match(i, i + char_len);
|
||||||
|
|
|
@ -715,7 +715,7 @@ impl<'a> Iterator for Wtf8CodePoints<'a> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
let (len, _) = self.bytes.size_hint();
|
let len = self.bytes.len();
|
||||||
(len.saturating_add(3) / 4, Some(len))
|
(len.saturating_add(3) / 4, Some(len))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue