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 3d143e0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add `serde` feature which provides implementations of `Serialize` and `Deserialize` for `Py<T>`. [#1366](https://github.com/PyO3/pyo3/pull/1366)
- Add FFI definition `_PyCFunctionFastWithKeywords` on Python 3.7 and up. [#1384](https://github.com/PyO3/pyo3/pull/1384)
- Add `PyDateTime::new_with_fold()` method. [#1398](https://github.com/PyO3/pyo3/pull/1398)
- Add `size_hint` impls for `{PyDict,PyList,PySet,PyTuple}Iterator`s. [#1699](https://github.com/PyO3/pyo3/pull/1699)

### Changed
- `prepare_freethreaded_python` will no longer register an `atexit` handler to call `Py_Finalize`. This resolves a number of issues with incompatible C extensions causing crashes at finalization. [#1355](https://github.com/PyO3/pyo3/pull/1355)
Expand Down
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 3d143e0

Please sign in to comment.