From 3d143e0dbb6b8a5486437ff034aadbb18907c58b Mon Sep 17 00:00:00 2001 From: Ohad Ravid Date: Sun, 27 Jun 2021 09:45:15 +0300 Subject: [PATCH] Added `size_hint` impls for `{PyDict,PyList,PySet,PyTuple}Iterator`s --- CHANGELOG.md | 1 + src/types/dict.rs | 6 ++++++ src/types/list.rs | 6 ++++++ src/types/set.rs | 6 ++++++ src/types/tuple.rs | 5 +++++ 5 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99a3e73b8e2..3994530c378 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. [#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) diff --git a/src/types/dict.rs b/src/types/dict.rs index abd0395894b..bc2b10bdf32 100644 --- a/src/types/dict.rs +++ b/src/types/dict.rs @@ -203,6 +203,12 @@ impl<'py> Iterator for PyDictIterator<'py> { } } } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + let len = self.dict.len().unwrap_or_default(); + (len, Some(len)) + } } impl<'a> std::iter::IntoIterator for &'a PyDict { diff --git a/src/types/list.rs b/src/types/list.rs index 4a0586e4fcd..eca886ad377 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -151,6 +151,12 @@ impl<'a> Iterator for PyListIterator<'a> { None } } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + let len = self.list.len(); + (len, Some(len)) + } } impl<'a> std::iter::IntoIterator for &'a PyList { diff --git a/src/types/set.rs b/src/types/set.rs index 3989edfb59e..7c2ea91be39 100644 --- a/src/types/set.rs +++ b/src/types/set.rs @@ -172,6 +172,12 @@ impl<'py> Iterator for PySetIterator<'py> { } } } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + let len = self.set.len().unwrap_or_default(); + (len, Some(len)) + } } impl<'a> std::iter::IntoIterator for &'a PySet { diff --git a/src/types/tuple.rs b/src/types/tuple.rs index 9dbe9395164..7e8859b0f6c 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -121,6 +121,11 @@ impl<'a> Iterator for PyTupleIterator<'a> { None } } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + (self.length, Some(self.length)) + } } impl<'a> IntoIterator for &'a PyTuple {