Skip to content

Commit

Permalink
Use len instead of size_hint where appropiate
Browse files Browse the repository at this point in the history
This makes it clearer that we're not just looking for a lower bound but
rather know that the iterator is an `ExactSizeIterator`.
  • Loading branch information
tbu- committed Jun 23, 2016
1 parent 4960f2f commit 8ff5c43
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/libcollections/vec.rs
Expand Up @@ -1658,7 +1658,7 @@ impl<T> Iterator for IntoIter<T> {

#[inline]
fn count(self) -> usize {
self.size_hint().0
self.len()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/fmt/mod.rs
Expand Up @@ -1384,7 +1384,7 @@ impl Debug for str {
for (i, c) in self.char_indices() {
let esc = c.escape_default();
// 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])?;
for c in esc {
f.write_char(c)?;
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/slice.rs
Expand Up @@ -835,7 +835,7 @@ macro_rules! iterator {

#[inline]
fn count(self) -> usize {
self.size_hint().0
self.len()
}

#[inline]
Expand Down Expand Up @@ -1444,7 +1444,7 @@ impl<'a, T> Iterator for Windows<'a, T> {

#[inline]
fn count(self) -> usize {
self.size_hint().0
self.len()
}

#[inline]
Expand Down Expand Up @@ -1541,7 +1541,7 @@ impl<'a, T> Iterator for Chunks<'a, T> {

#[inline]
fn count(self) -> usize {
self.size_hint().0
self.len()
}

#[inline]
Expand Down Expand Up @@ -1632,7 +1632,7 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {

#[inline]
fn count(self) -> usize {
self.size_hint().0
self.len()
}

#[inline]
Expand Down
9 changes: 4 additions & 5 deletions src/libcore/str/mod.rs
Expand Up @@ -433,7 +433,7 @@ impl<'a> Iterator for Chars<'a> {

#[inline]
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`
// belongs to a slice in memory which has a maximum length of
// `isize::MAX` (that's well below `usize::MAX`).
Expand Down Expand Up @@ -480,12 +480,12 @@ impl<'a> Iterator for CharIndices<'a> {

#[inline]
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() {
None => None,
Some(ch) => {
let index = self.front_offset;
let (len, _) = self.iter.iter.size_hint();
let len = self.iter.iter.len();
self.front_offset += pre_len - len;
Some((index, ch))
}
Expand All @@ -505,8 +505,7 @@ impl<'a> DoubleEndedIterator for CharIndices<'a> {
match self.iter.next_back() {
None => None,
Some(ch) => {
let (len, _) = self.iter.iter.size_hint();
let index = self.front_offset + len;
let index = self.front_offset + self.iter.iter.len();
Some((index, ch))
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/str/pattern.rs
Expand Up @@ -310,9 +310,9 @@ unsafe impl<'a, C: CharEq> Searcher<'a> for CharEqSearcher<'a, C> {
let s = &mut self.char_indices;
// Compare lengths of the internal byte slice iterator
// 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() {
let (len, _) = s.iter.iter.size_hint();
let len = s.iter.iter.len();
let char_len = pre_len - len;
if self.char_eq.matches(c) {
return SearchStep::Match(i, i + char_len);
Expand All @@ -330,9 +330,9 @@ unsafe impl<'a, C: CharEq> ReverseSearcher<'a> for CharEqSearcher<'a, C> {
let s = &mut self.char_indices;
// Compare lengths of the internal byte slice iterator
// 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() {
let (len, _) = s.iter.iter.size_hint();
let len = s.iter.iter.len();
let char_len = pre_len - len;
if self.char_eq.matches(c) {
return SearchStep::Match(i, i + char_len);
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/common/wtf8.rs
Expand Up @@ -715,7 +715,7 @@ impl<'a> Iterator for Wtf8CodePoints<'a> {

#[inline]
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))
}
}
Expand Down

0 comments on commit 8ff5c43

Please sign in to comment.