expose `BoundDictIterator` and `BoundListIterator`

This commit is contained in:
David Hewitt 2023-12-21 13:09:22 +00:00
parent e8e6fb93d7
commit 3092289020
3 changed files with 28 additions and 27 deletions

View File

@ -335,7 +335,7 @@ pub trait PyDictMethods<'py> {
/// If PyO3 detects that the dictionary is mutated during iteration, it will panic.
/// It is allowed to modify values as you iterate over the dictionary, but only
/// so long as the set of keys does not change.
fn iter(&self) -> PyDictIterator2<'py>;
fn iter(&self) -> BoundDictIterator<'py>;
/// Returns `self` cast as a `PyMapping`.
fn as_mapping(&self) -> &Bound<'py, PyMapping>;
@ -478,8 +478,8 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
}
}
fn iter(&self) -> PyDictIterator2<'py> {
PyDictIterator2::new(self.clone())
fn iter(&self) -> BoundDictIterator<'py> {
BoundDictIterator::new(self.clone())
}
fn as_mapping(&self) -> &Bound<'py, PyMapping> {
@ -512,7 +512,7 @@ fn dict_len(dict: &Bound<'_, PyDict>) -> Py_ssize_t {
}
/// PyO3 implementation of an iterator for a Python `dict` object.
pub struct PyDictIterator<'py>(PyDictIterator2<'py>);
pub struct PyDictIterator<'py>(BoundDictIterator<'py>);
impl<'py> Iterator for PyDictIterator<'py> {
type Item = (&'py PyAny, &'py PyAny);
@ -545,14 +545,14 @@ impl<'a> IntoIterator for &'a PyDict {
}
/// PyO3 implementation of an iterator for a Python `dict` object.
pub struct PyDictIterator2<'py> {
pub struct BoundDictIterator<'py> {
dict: Bound<'py, PyDict>,
ppos: ffi::Py_ssize_t,
di_used: ffi::Py_ssize_t,
len: ffi::Py_ssize_t,
}
impl<'py> Iterator for PyDictIterator2<'py> {
impl<'py> Iterator for BoundDictIterator<'py> {
type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>);
#[inline]
@ -610,16 +610,16 @@ impl<'py> Iterator for PyDictIterator2<'py> {
}
}
impl<'py> ExactSizeIterator for PyDictIterator2<'py> {
impl<'py> ExactSizeIterator for BoundDictIterator<'py> {
fn len(&self) -> usize {
self.len as usize
}
}
impl<'py> PyDictIterator2<'py> {
impl<'py> BoundDictIterator<'py> {
fn new(dict: Bound<'py, PyDict>) -> Self {
let len = dict_len(&dict);
PyDictIterator2 {
BoundDictIterator {
dict,
ppos: 0,
di_used: len,
@ -630,7 +630,7 @@ impl<'py> PyDictIterator2<'py> {
impl<'py> IntoIterator for &'_ Bound<'py, PyDict> {
type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>);
type IntoIter = PyDictIterator2<'py>;
type IntoIter = BoundDictIterator<'py>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
@ -639,10 +639,10 @@ impl<'py> IntoIterator for &'_ Bound<'py, PyDict> {
impl<'py> IntoIterator for Bound<'py, PyDict> {
type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>);
type IntoIter = PyDictIterator2<'py>;
type IntoIter = BoundDictIterator<'py>;
fn into_iter(self) -> Self::IntoIter {
PyDictIterator2::new(self)
BoundDictIterator::new(self)
}
}

View File

@ -341,7 +341,7 @@ pub trait PyListMethods<'py> {
V: ToPyObject;
/// Returns an iterator over this list's items.
fn iter(&self) -> PyListIterator2<'py>;
fn iter(&self) -> BoundListIterator<'py>;
/// Sorts the list in-place. Equivalent to the Python expression `l.sort()`.
fn sort(&self) -> PyResult<()>;
@ -526,8 +526,8 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> {
}
/// Returns an iterator over this list's items.
fn iter(&self) -> PyListIterator2<'py> {
PyListIterator2::new(self.clone())
fn iter(&self) -> BoundListIterator<'py> {
BoundListIterator::new(self.clone())
}
/// Sorts the list in-place. Equivalent to the Python expression `l.sort()`.
@ -553,7 +553,7 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> {
}
/// Used by `PyList::iter()`.
pub struct PyListIterator<'a>(PyListIterator2<'a>);
pub struct PyListIterator<'a>(BoundListIterator<'a>);
impl<'a> Iterator for PyListIterator<'a> {
type Item = &'a PyAny;
@ -594,16 +594,16 @@ impl<'a> IntoIterator for &'a PyList {
}
/// Used by `PyList::iter()`.
pub struct PyListIterator2<'py> {
pub struct BoundListIterator<'py> {
list: Bound<'py, PyList>,
index: usize,
length: usize,
}
impl<'py> PyListIterator2<'py> {
impl<'py> BoundListIterator<'py> {
fn new(list: Bound<'py, PyList>) -> Self {
let length: usize = list.len();
PyListIterator2 {
BoundListIterator {
list,
index: 0,
length,
@ -619,7 +619,7 @@ impl<'py> PyListIterator2<'py> {
}
}
impl<'py> Iterator for PyListIterator2<'py> {
impl<'py> Iterator for BoundListIterator<'py> {
type Item = Bound<'py, PyAny>;
#[inline]
@ -642,7 +642,7 @@ impl<'py> Iterator for PyListIterator2<'py> {
}
}
impl DoubleEndedIterator for PyListIterator2<'_> {
impl DoubleEndedIterator for BoundListIterator<'_> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let length = self.length.min(self.list.len());
@ -657,17 +657,17 @@ impl DoubleEndedIterator for PyListIterator2<'_> {
}
}
impl ExactSizeIterator for PyListIterator2<'_> {
impl ExactSizeIterator for BoundListIterator<'_> {
fn len(&self) -> usize {
self.length.saturating_sub(self.index)
}
}
impl FusedIterator for PyListIterator2<'_> {}
impl FusedIterator for BoundListIterator<'_> {}
impl<'a, 'py> IntoIterator for &'a Bound<'py, PyList> {
type Item = Bound<'py, PyAny>;
type IntoIter = PyListIterator2<'py>;
type IntoIter = BoundListIterator<'py>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
@ -676,10 +676,10 @@ impl<'a, 'py> IntoIterator for &'a Bound<'py, PyList> {
impl<'py> IntoIterator for Bound<'py, PyList> {
type Item = Bound<'py, PyAny>;
type IntoIter = PyListIterator2<'py>;
type IntoIter = BoundListIterator<'py>;
fn into_iter(self) -> Self::IntoIter {
PyListIterator2::new(self)
BoundListIterator::new(self)
}
}

View File

@ -76,8 +76,9 @@ pub use self::typeobject::PyType;
/// the Limited API and PyPy, the underlying structures are opaque and that may not be possible.
/// In these cases the iterators are implemented by forwarding to [`PyIterator`].
pub mod iter {
pub use super::dict::PyDictIterator;
pub use super::dict::{BoundDictIterator, PyDictIterator};
pub use super::frozenset::PyFrozenSetIterator;
pub use super::list::{BoundListIterator, PyListIterator};
pub use super::set::PySetIterator;
pub use super::tuple::PyTupleIterator;
}