Skip to content

Commit

Permalink
Added size_hint impls for {PyDict,PyList,PySet,PyTuple}Iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
ohadravid committed Jun 27, 2021
1 parent 190eb72 commit d796aa6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ impl<'py> Iterator for PyDictIterator<'py> {
}
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.dict.len().unwrap_or_default();
(len, Some(len))
}
}

impl<'a> std::iter::IntoIterator for &'a PyDict {
Expand Down
6 changes: 6 additions & 0 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ impl<'a> Iterator for PyListIterator<'a> {
None
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.list.len();
(len, Some(len))
}
}

impl<'a> std::iter::IntoIterator for &'a PyList {
Expand Down
6 changes: 6 additions & 0 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ impl<'py> Iterator for PySetIterator<'py> {
}
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.set.len().unwrap_or_default();
(len, Some(len))
}
}

impl<'a> std::iter::IntoIterator for &'a PySet {
Expand Down
5 changes: 5 additions & 0 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ impl<'a> Iterator for PyTupleIterator<'a> {
None
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.length, Some(self.length))
}
}

impl<'a> IntoIterator for &'a PyTuple {
Expand Down

0 comments on commit d796aa6

Please sign in to comment.