Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/5725.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove redundant internal counters from `BoundSetIterator` and `BoundFrozenSetIterator`.
21 changes: 8 additions & 13 deletions src/types/frozenset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,11 @@ impl<'py> IntoIterator for &Bound<'py, PyFrozenSet> {
}

/// PyO3 implementation of an iterator for a Python `frozenset` object.
pub struct BoundFrozenSetIterator<'p> {
it: Bound<'p, PyIterator>,
// Remaining elements in the frozenset
remaining: usize,
}
pub struct BoundFrozenSetIterator<'py>(Bound<'py, PyIterator>);

impl<'py> BoundFrozenSetIterator<'py> {
pub(super) fn new(set: Bound<'py, PyFrozenSet>) -> Self {
Self {
it: PyIterator::from_object(&set).unwrap(),
remaining: set.len(),
}
Self(PyIterator::from_object(&set).expect("frozenset should always be iterable"))
}
}

Expand All @@ -208,12 +201,14 @@ impl<'py> Iterator for BoundFrozenSetIterator<'py> {

/// Advances the iterator and returns the next value.
fn next(&mut self) -> Option<Self::Item> {
self.remaining = self.remaining.saturating_sub(1);
self.it.next().map(Result::unwrap)
self.0
.next()
.map(|result| result.expect("frozenset iteration should be infallible"))
}

fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
let len = ExactSizeIterator::len(self);
(len, Some(len))
}

#[inline]
Expand All @@ -227,7 +222,7 @@ impl<'py> Iterator for BoundFrozenSetIterator<'py> {

impl ExactSizeIterator for BoundFrozenSetIterator<'_> {
fn len(&self) -> usize {
self.remaining
self.0.size_hint().0
}
}

Expand Down
22 changes: 8 additions & 14 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,11 @@ impl<'py> IntoIterator for &Bound<'py, PySet> {
}

/// PyO3 implementation of an iterator for a Python `set` object.
pub struct BoundSetIterator<'p> {
it: Bound<'p, PyIterator>,
// Remaining elements in the set. This is fine to store because
// Python will error if the set changes size during iteration.
remaining: usize,
}
pub struct BoundSetIterator<'py>(Bound<'py, PyIterator>);

impl<'py> BoundSetIterator<'py> {
pub(super) fn new(set: Bound<'py, PySet>) -> Self {
Self {
it: PyIterator::from_object(&set).unwrap(),
remaining: set.len(),
}
Self(PyIterator::from_object(&set).expect("set should always be iterable"))
}
}

Expand All @@ -243,12 +235,14 @@ impl<'py> Iterator for BoundSetIterator<'py> {

/// Advances the iterator and returns the next value.
fn next(&mut self) -> Option<Self::Item> {
self.remaining = self.remaining.saturating_sub(1);
self.it.next().map(Result::unwrap)
self.0
.next()
.map(|result| result.expect("set iteration should be infallible"))
}

fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
let len = ExactSizeIterator::len(self);
(len, Some(len))
}

#[inline]
Expand All @@ -262,7 +256,7 @@ impl<'py> Iterator for BoundSetIterator<'py> {

impl ExactSizeIterator for BoundSetIterator<'_> {
fn len(&self) -> usize {
self.remaining
self.0.size_hint().0
}
}

Expand Down
Loading