-
Notifications
You must be signed in to change notification settings - Fork 758
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 IntoIterator for PySet and PyFrozenSet #716
Conversation
Could you please rewrite the implementation using
#[cfg(not(Py_LIMITED_API))]
extern "C" {
#[cfg_attr(PyPy, link_name = "_PySet_NextEntry")]
pub fn _PySet_NextEntry(
set: *mut PyObject,
pos: *mut Py_ssize_t,
key: *mut *mut PyObject,
hash: *mut super::Py_hash_t,
) -> c_int;
}
#[cfg(not(Py_LIMITED_API))]
pub struct PySetIterator<'py> {
set: &'py super::PyAny,
pos: isize,
}
#[cfg(not(Py_LIMITED_API))]
impl<'py> Iterator for PySetIterator<'py> {
type Item = &'py super::PyAny;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
unsafe {
let mut key: *mut ffi::PyObject = std::ptr::null_mut();
let mut hash: ffi::Py_hash_t = 0;
if ffi::_PySet_NextEntry(self.set.as_ptr(), &mut self.pos, &mut key, &mut hash) != 0 {
Some(self.set.py().from_borrowed_ptr(key))
} else {
None
}
}
}
}
#[cfg(not(Py_LIMITED_API))]
impl<'a> std::iter::IntoIterator for &'a PySet {
type Item = &'a PyAny;
type IntoIter = PySetIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
PySetIterator {
set: self.as_ref(),
pos: 0,
}
}
} |
👍 Thanks - I missed |
Thank you! |
Thanks for fixing this. My only concern is that For a list (ignoring the comment that I guess is left over from copying the method from tuples) we have: Lines 116 to 122 in b3ca27d
Would it help to add |
I left If you still would like to have |
okay, I'll leave it. |
@samuelcolvin |
Closes #715