Merge pull request #3366 from PyO3/tuple-rev

Implement DoubleEndedIterator for PyTupleIterator
This commit is contained in:
Adam Reichold 2023-08-08 04:06:15 +00:00 committed by GitHub
commit 28b8623585
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 127 additions and 17 deletions

View File

@ -0,0 +1 @@
Add implementations `DoubleEndedIterator` for `PyTupleIterator` and `PyListIterator`.

View File

@ -0,0 +1 @@
The `PyTupleIterator` type returned by `PyTuple::iter` is now public and hence can be named by downstream crates.

View File

@ -495,7 +495,7 @@ impl<T: Element> PyBuffer<T> {
err::error_on_minusone(py, unsafe {
ffi::PyBuffer_ToContiguous(
target.as_ptr() as *mut raw::c_void,
target.as_mut_ptr().cast(),
#[cfg(Py_3_11)]
&*self.0,
#[cfg(not(Py_3_11))]

View File

@ -1,4 +1,5 @@
use std::convert::TryInto;
use std::iter::FusedIterator;
use crate::err::{self, PyResult};
use crate::ffi::{self, Py_ssize_t};
@ -264,6 +265,7 @@ impl PyList {
PyListIterator {
list: self,
index: 0,
length: self.len(),
}
}
@ -291,18 +293,28 @@ index_impls!(PyList, "list", PyList::len, PyList::get_slice);
pub struct PyListIterator<'a> {
list: &'a PyList,
index: usize,
length: usize,
}
impl<'a> PyListIterator<'a> {
unsafe fn get_item(&self, index: usize) -> &'a PyAny {
#[cfg(any(Py_LIMITED_API, PyPy))]
let item = self.list.get_item(index).expect("list.get failed");
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
let item = self.list.get_item_unchecked(index);
item
}
}
impl<'a> Iterator for PyListIterator<'a> {
type Item = &'a PyAny;
#[inline]
fn next(&mut self) -> Option<&'a PyAny> {
if self.index < self.list.len() {
#[cfg(any(Py_LIMITED_API, PyPy))]
let item = self.list.get_item(self.index).expect("list.get failed");
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
let item = unsafe { self.list.get_item_unchecked(self.index) };
fn next(&mut self) -> Option<Self::Item> {
let length = self.length.min(self.list.len());
if self.index < length {
let item = unsafe { self.get_item(self.index) };
self.index += 1;
Some(item)
} else {
@ -317,13 +329,30 @@ impl<'a> Iterator for PyListIterator<'a> {
}
}
impl<'a> ExactSizeIterator for PyListIterator<'a> {
fn len(&self) -> usize {
self.list.len().saturating_sub(self.index)
impl<'a> DoubleEndedIterator for PyListIterator<'a> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let length = self.length.min(self.list.len());
if self.index < length {
let item = unsafe { self.get_item(length - 1) };
self.length = length - 1;
Some(item)
} else {
None
}
}
}
impl<'a> std::iter::IntoIterator for &'a PyList {
impl<'a> ExactSizeIterator for PyListIterator<'a> {
fn len(&self) -> usize {
self.length.saturating_sub(self.index)
}
}
impl FusedIterator for PyListIterator<'_> {}
impl<'a> IntoIterator for &'a PyList {
type Item = &'a PyAny;
type IntoIter = PyListIterator<'a>;
@ -494,13 +523,41 @@ mod tests {
iter.next();
assert_eq!(iter.size_hint(), (v.len() - 1, Some(v.len() - 1)));
// Exhust iterator.
// Exhaust iterator.
for _ in &mut iter {}
assert_eq!(iter.size_hint(), (0, Some(0)));
});
}
#[test]
fn test_iter_rev() {
Python::with_gil(|py| {
let v = vec![2, 3, 5, 7];
let ob = v.to_object(py);
let list: &PyList = ob.downcast(py).unwrap();
let mut iter = list.iter().rev();
assert_eq!(iter.size_hint(), (4, Some(4)));
assert_eq!(iter.next().unwrap().extract::<i32>().unwrap(), 7);
assert_eq!(iter.size_hint(), (3, Some(3)));
assert_eq!(iter.next().unwrap().extract::<i32>().unwrap(), 5);
assert_eq!(iter.size_hint(), (2, Some(2)));
assert_eq!(iter.next().unwrap().extract::<i32>().unwrap(), 3);
assert_eq!(iter.size_hint(), (1, Some(1)));
assert_eq!(iter.next().unwrap().extract::<i32>().unwrap(), 2);
assert_eq!(iter.size_hint(), (0, Some(0)));
assert!(iter.next().is_none());
assert!(iter.next().is_none());
});
}
#[test]
fn test_into_iter() {
Python::with_gil(|py| {

View File

@ -75,6 +75,7 @@ pub mod iter {
pub use super::dict::PyDictIterator;
pub use super::frozenset::PyFrozenSetIterator;
pub use super::set::PySetIterator;
pub use super::tuple::PyTupleIterator;
}
// Implementations core to all native types

View File

@ -1,4 +1,5 @@
use std::convert::TryInto;
use std::iter::FusedIterator;
use crate::ffi::{self, Py_ssize_t};
#[cfg(feature = "experimental-inspect")]
@ -232,16 +233,23 @@ pub struct PyTupleIterator<'a> {
length: usize,
}
impl<'a> PyTupleIterator<'a> {
unsafe fn get_item(&self, index: usize) -> &'a PyAny {
#[cfg(any(Py_LIMITED_API, PyPy))]
let item = self.tuple.get_item(index).expect("tuple.get failed");
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
let item = self.tuple.get_item_unchecked(index);
item
}
}
impl<'a> Iterator for PyTupleIterator<'a> {
type Item = &'a PyAny;
#[inline]
fn next(&mut self) -> Option<&'a PyAny> {
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.length {
#[cfg(any(Py_LIMITED_API, PyPy))]
let item = self.tuple.get_item(self.index).expect("tuple.get failed");
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
let item = unsafe { self.tuple.get_item_unchecked(self.index) };
let item = unsafe { self.get_item(self.index) };
self.index += 1;
Some(item)
} else {
@ -256,12 +264,27 @@ impl<'a> Iterator for PyTupleIterator<'a> {
}
}
impl<'a> DoubleEndedIterator for PyTupleIterator<'a> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
if self.index < self.length {
let item = unsafe { self.get_item(self.length - 1) };
self.length -= 1;
Some(item)
} else {
None
}
}
}
impl<'a> ExactSizeIterator for PyTupleIterator<'a> {
fn len(&self) -> usize {
self.length.saturating_sub(self.index)
}
}
impl FusedIterator for PyTupleIterator<'_> {}
impl<'a> IntoIterator for &'a PyTuple {
type Item = &'a PyAny;
type IntoIter = PyTupleIterator<'a>;
@ -510,6 +533,33 @@ mod tests {
assert_eq!(3_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (0, Some(0)));
assert!(iter.next().is_none());
assert!(iter.next().is_none());
});
}
#[test]
fn test_iter_rev() {
Python::with_gil(|py| {
let ob = (1, 2, 3).to_object(py);
let tuple: &PyTuple = ob.downcast(py).unwrap();
assert_eq!(3, tuple.len());
let mut iter = tuple.iter().rev();
assert_eq!(iter.size_hint(), (3, Some(3)));
assert_eq!(3_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (2, Some(2)));
assert_eq!(2_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (1, Some(1)));
assert_eq!(1_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (0, Some(0)));
assert!(iter.next().is_none());
assert!(iter.next().is_none());
});
}