Skip to content

Commit 0f4c7ca

Browse files
authored
remove redundant counters for remaining elements in set and frozenset iteration (#5726)
* don't count remaining elements in set and frozenset iteration * make call to `ExactSizeIterator` implementation more explicit * newsfragment
1 parent b527ee8 commit 0f4c7ca

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

newsfragments/5725.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove redundant internal counters from `BoundSetIterator` and `BoundFrozenSetIterator`.

src/types/frozenset.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,11 @@ impl<'py> IntoIterator for &Bound<'py, PyFrozenSet> {
188188
}
189189

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

197193
impl<'py> BoundFrozenSetIterator<'py> {
198194
pub(super) fn new(set: Bound<'py, PyFrozenSet>) -> Self {
199-
Self {
200-
it: PyIterator::from_object(&set).unwrap(),
201-
remaining: set.len(),
202-
}
195+
Self(PyIterator::from_object(&set).expect("frozenset should always be iterable"))
203196
}
204197
}
205198

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

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

215209
fn size_hint(&self) -> (usize, Option<usize>) {
216-
(self.remaining, Some(self.remaining))
210+
let len = ExactSizeIterator::len(self);
211+
(len, Some(len))
217212
}
218213

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

228223
impl ExactSizeIterator for BoundFrozenSetIterator<'_> {
229224
fn len(&self) -> usize {
230-
self.remaining
225+
self.0.size_hint().0
231226
}
232227
}
233228

src/types/set.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,11 @@ impl<'py> IntoIterator for &Bound<'py, PySet> {
222222
}
223223

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

232227
impl<'py> BoundSetIterator<'py> {
233228
pub(super) fn new(set: Bound<'py, PySet>) -> Self {
234-
Self {
235-
it: PyIterator::from_object(&set).unwrap(),
236-
remaining: set.len(),
237-
}
229+
Self(PyIterator::from_object(&set).expect("set should always be iterable"))
238230
}
239231
}
240232

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

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

250243
fn size_hint(&self) -> (usize, Option<usize>) {
251-
(self.remaining, Some(self.remaining))
244+
let len = ExactSizeIterator::len(self);
245+
(len, Some(len))
252246
}
253247

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

263257
impl ExactSizeIterator for BoundSetIterator<'_> {
264258
fn len(&self) -> usize {
265-
self.remaining
259+
self.0.size_hint().0
266260
}
267261
}
268262

0 commit comments

Comments
 (0)