Skip to content

Commit

Permalink
Implement DoubleEndedIterator for PyTupleIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Aug 3, 2023
1 parent ae982b8 commit 699b263
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions newsfragments/3366.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add implementation `DoubleEndedIterator` for `PyTupleIterator`.
2 changes: 1 addition & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() as *mut raw::c_void,
#[cfg(Py_3_11)]
&*self.0,
#[cfg(not(Py_3_11))]
Expand Down
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 45 additions & 5 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,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 {
Expand All @@ -256,6 +263,18 @@ impl<'a> Iterator for PyTupleIterator<'a> {
}
}

impl<'a> DoubleEndedIterator for PyTupleIterator<'a> {
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)
Expand Down Expand Up @@ -513,6 +532,27 @@ mod tests {
});
}

#[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)));
});
}

#[test]
fn test_into_iter() {
Python::with_gil(|py| {
Expand Down

0 comments on commit 699b263

Please sign in to comment.