Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement iter for PySet and PyFrozenSet #717

Merged
merged 3 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,20 @@ impl PyDict {
}
}

/// Returns a iterator of (key, value) pairs in this dictionary
/// Note that it's unsafe to use when the dictionary might be changed
/// by other python code.
/// Returns a iterator of (key, value) pairs in this dictionary.
///
/// Note that it's unsafe to use when the dictionary might be changed by other code.
pub fn iter(&self) -> PyDictIterator {
let py = self.py();
PyDictIterator {
dict: self.to_object(py),
dict: self.as_ref(),
pos: 0,
py,
}
}
}

pub struct PyDictIterator<'py> {
dict: PyObject,
dict: &'py PyAny,
pos: isize,
py: Python<'py>,
}

impl<'py> Iterator for PyDictIterator<'py> {
Expand All @@ -178,7 +175,7 @@ impl<'py> Iterator for PyDictIterator<'py> {
let mut key: *mut ffi::PyObject = std::ptr::null_mut();
let mut value: *mut ffi::PyObject = std::ptr::null_mut();
if ffi::PyDict_Next(self.dict.as_ptr(), &mut self.pos, &mut key, &mut value) != 0 {
let py = self.py;
let py = self.dict.py();
Some((py.from_borrowed_ptr(key), py.from_borrowed_ptr(value)))
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl PyList {
})
}

/// Returns an iterator over the tuple items.
/// Returns an iterator over this list items.
pub fn iter(&self) -> PyListIterator {
PyListIterator {
list: self,
Expand Down
42 changes: 29 additions & 13 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ impl PySet {
pub fn pop(&self) -> Option<PyObject> {
unsafe { PyObject::from_owned_ptr_or_opt(self.py(), ffi::PySet_Pop(self.as_ptr())) }
}

/// Returns an iterator of values in this set.
///
/// Note that it can be unsafe to use when the set might be changed by other code.
#[cfg(not(Py_LIMITED_API))]
pub fn iter(&self) -> PySetIterator {
PySetIterator {
set: self.as_ref(),
pos: 0,
}
}
}

#[cfg(not(Py_LIMITED_API))]
Expand Down Expand Up @@ -127,10 +138,7 @@ impl<'a> std::iter::IntoIterator for &'a PySet {
type IntoIter = PySetIterator<'a>;

fn into_iter(self) -> Self::IntoIter {
PySetIterator {
set: self.as_ref(),
pos: 0,
}
self.iter()
}
}

Expand Down Expand Up @@ -204,7 +212,16 @@ impl PyFrozenSet {
}
})
}

/// Returns an iterator of values in this frozen set.
///
/// Note that it can be unsafe to use when the set might be changed by other code.
#[cfg(not(Py_LIMITED_API))]
pub fn iter(&self) -> PySetIterator {
self.into_iter()
}
}

#[cfg(not(Py_LIMITED_API))]
impl<'a> std::iter::IntoIterator for &'a PyFrozenSet {
type Item = &'a PyAny;
Expand All @@ -222,9 +239,7 @@ impl<'a> std::iter::IntoIterator for &'a PyFrozenSet {
mod test {
use super::{PyFrozenSet, PySet};
use crate::instance::AsPyRef;
use crate::objectprotocol::ObjectProtocol;
use crate::Python;
use crate::{PyTryFrom, ToPyObject};
use crate::{ObjectProtocol, PyTryFrom, Python, ToPyObject};
use std::collections::HashSet;

#[test]
Expand Down Expand Up @@ -317,9 +332,10 @@ mod test {
let py = gil.python();

let set = PySet::new(py, &[1]).unwrap();
// objectprotocol iteration
for el in set.iter().unwrap() {
assert_eq!(1i32, el.unwrap().extract::<i32>().unwrap());

// iter method
for el in set.iter() {
assert_eq!(1i32, el.extract().unwrap());
}

// intoiterator iteration
Expand Down Expand Up @@ -363,9 +379,9 @@ mod test {

let set = PyFrozenSet::new(py, &[1]).unwrap();

// objectprotocol iteration
for el in set.iter().unwrap() {
assert_eq!(1i32, el.unwrap().extract::<i32>().unwrap());
// iter method
for el in set.iter() {
assert_eq!(1i32, el.extract::<i32>().unwrap());
}

// intoiterator iteration
Expand Down