File tree Expand file tree Collapse file tree 6 files changed +36
-23
lines changed Expand file tree Collapse file tree 6 files changed +36
-23
lines changed Original file line number Diff line number Diff line change
1
+ Implemented ` ExactSizeIterator ` for ` PyListIterator ` , ` PyDictIterator ` , ` PySetIterator ` and ` PyFrozenSetIterator `
Original file line number Diff line number Diff line change @@ -310,11 +310,17 @@ impl<'py> Iterator for PyDictIterator<'py> {
310
310
311
311
#[ inline]
312
312
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
313
- let len = self . len as usize ;
313
+ let len = self . len ( ) ;
314
314
( len, Some ( len) )
315
315
}
316
316
}
317
317
318
+ impl < ' py > ExactSizeIterator for PyDictIterator < ' py > {
319
+ fn len ( & self ) -> usize {
320
+ self . len as usize
321
+ }
322
+ }
323
+
318
324
impl < ' a > std:: iter:: IntoIterator for & ' a PyDict {
319
325
type Item = ( & ' a PyAny , & ' a PyAny ) ;
320
326
type IntoIter = PyDictIterator < ' a > ;
Original file line number Diff line number Diff line change @@ -119,7 +119,7 @@ mod impl_ {
119
119
120
120
/// PyO3 implementation of an iterator for a Python `frozenset` object.
121
121
pub struct PyFrozenSetIterator < ' py > {
122
- set : & ' py PyAny ,
122
+ set : & ' py PyFrozenSet ,
123
123
pos : ffi:: Py_ssize_t ,
124
124
}
125
125
@@ -143,11 +143,14 @@ mod impl_ {
143
143
144
144
#[ inline]
145
145
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
146
- let len = self . set . len ( ) . unwrap_or_default ( ) ;
147
- (
148
- len. saturating_sub ( self . pos as usize ) ,
149
- Some ( len. saturating_sub ( self . pos as usize ) ) ,
150
- )
146
+ let len = self . len ( ) ;
147
+ ( len, Some ( len) )
148
+ }
149
+ }
150
+
151
+ impl < ' py > ExactSizeIterator for PyFrozenSetIterator < ' py > {
152
+ fn len ( & self ) -> usize {
153
+ self . set . len ( ) . saturating_sub ( self . pos as usize )
151
154
}
152
155
}
153
156
}
Original file line number Diff line number Diff line change @@ -322,12 +322,14 @@ impl<'a> Iterator for PyListIterator<'a> {
322
322
323
323
#[ inline]
324
324
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
325
- let len = self . list . len ( ) ;
325
+ let len = self . len ( ) ;
326
+ ( len, Some ( len) )
327
+ }
328
+ }
326
329
327
- (
328
- len. saturating_sub ( self . index ) ,
329
- Some ( len. saturating_sub ( self . index ) ) ,
330
- )
330
+ impl < ' a > ExactSizeIterator for PyListIterator < ' a > {
331
+ fn len ( & self ) -> usize {
332
+ self . list . len ( ) . saturating_sub ( self . index )
331
333
}
332
334
}
333
335
Original file line number Diff line number Diff line change @@ -169,7 +169,7 @@ mod impl_ {
169
169
170
170
/// PyO3 implementation of an iterator for a Python `set` object.
171
171
pub struct PySetIterator < ' py > {
172
- set : & ' py super :: PyAny ,
172
+ set : & ' py super :: PySet ,
173
173
pos : ffi:: Py_ssize_t ,
174
174
used : ffi:: Py_ssize_t ,
175
175
}
@@ -219,11 +219,14 @@ mod impl_ {
219
219
220
220
#[ inline]
221
221
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
222
- let len = self . set . len ( ) . unwrap_or_default ( ) ;
223
- (
224
- len. saturating_sub ( self . pos as usize ) ,
225
- Some ( len. saturating_sub ( self . pos as usize ) ) ,
226
- )
222
+ let len = self . len ( ) ;
223
+ ( len, Some ( len) )
224
+ }
225
+ }
226
+
227
+ impl < ' py > ExactSizeIterator for PySetIterator < ' py > {
228
+ fn len ( & self ) -> usize {
229
+ self . set . len ( ) . saturating_sub ( self . pos as usize )
227
230
}
228
231
}
229
232
}
Original file line number Diff line number Diff line change @@ -242,16 +242,14 @@ impl<'a> Iterator for PyTupleIterator<'a> {
242
242
243
243
#[ inline]
244
244
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
245
- (
246
- self . length . saturating_sub ( self . index as usize ) ,
247
- Some ( self . length . saturating_sub ( self . index as usize ) ) ,
248
- )
245
+ let len = self . len ( ) ;
246
+ ( len, Some ( len) )
249
247
}
250
248
}
251
249
252
250
impl < ' a > ExactSizeIterator for PyTupleIterator < ' a > {
253
251
fn len ( & self ) -> usize {
254
- self . length - self . index
252
+ self . length . saturating_sub ( self . index )
255
253
}
256
254
}
257
255
You can’t perform that action at this time.
0 commit comments