Skip to content

Commit

Permalink
expose BoundDictIterator and BoundListIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 21, 2023
1 parent e8e6fb9 commit 3092289
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
24 changes: 12 additions & 12 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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)
}
}

Expand Down
28 changes: 14 additions & 14 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>;
Expand Down Expand Up @@ -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()`.
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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]
Expand All @@ -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());
Expand All @@ -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()
Expand All @@ -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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 3092289

Please sign in to comment.