Skip to content

Commit

Permalink
Add more implementations of ExactSizeIterator when iterating built-in…
Browse files Browse the repository at this point in the history
… Python data structures.
  • Loading branch information
adamreichold authored and davidhewitt committed Oct 30, 2022
1 parent ba82788 commit e48525f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 deletions.
1 change: 1 addition & 0 deletions newsfragments/2676.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implemented `ExactSizeIterator` for `PyListIterator`, `PyDictIterator`, `PySetIterator` and `PyFrozenSetIterator`
8 changes: 7 additions & 1 deletion src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,17 @@ impl<'py> Iterator for PyDictIterator<'py> {

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

impl<'py> ExactSizeIterator for PyDictIterator<'py> {
fn len(&self) -> usize {
self.len as usize
}
}

impl<'a> std::iter::IntoIterator for &'a PyDict {
type Item = (&'a PyAny, &'a PyAny);
type IntoIter = PyDictIterator<'a>;
Expand Down
15 changes: 9 additions & 6 deletions src/types/frozenset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod impl_ {
}

pub struct PyFrozenSetIterator<'py> {
set: &'py PyAny,
set: &'py PyFrozenSet,
pos: ffi::Py_ssize_t,
}

Expand All @@ -141,11 +141,14 @@ mod impl_ {

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

impl<'py> ExactSizeIterator for PyFrozenSetIterator<'py> {
fn len(&self) -> usize {
self.set.len().saturating_sub(self.pos as usize)
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,14 @@ impl<'a> Iterator for PyListIterator<'a> {

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

(
len.saturating_sub(self.index),
Some(len.saturating_sub(self.index)),
)
impl<'a> ExactSizeIterator for PyListIterator<'a> {
fn len(&self) -> usize {
self.list.len().saturating_sub(self.index)
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod impl_ {
mod impl_ {
use super::*;
pub struct PySetIterator<'py> {
set: &'py super::PyAny,
set: &'py super::PySet,
pos: ffi::Py_ssize_t,
used: ffi::Py_ssize_t,
}
Expand Down Expand Up @@ -215,11 +215,14 @@ mod impl_ {

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

impl<'py> ExactSizeIterator for PySetIterator<'py> {
fn len(&self) -> usize {
self.set.len().saturating_sub(self.pos as usize)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,14 @@ impl<'a> Iterator for PyTupleIterator<'a> {

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

impl<'a> ExactSizeIterator for PyTupleIterator<'a> {
fn len(&self) -> usize {
self.length - self.index
self.length.saturating_sub(self.index)
}
}

Expand Down

0 comments on commit e48525f

Please sign in to comment.