Use &PyAny instead of PyObject in PyDictIterator

This commit is contained in:
kngwyu 2020-01-08 23:43:26 +09:00
parent 026caeda68
commit 7b502821ce
2 changed files with 8 additions and 11 deletions

View File

@ -154,19 +154,16 @@ impl PyDict {
/// ///
/// Note that it's unsafe to use when the dictionary might be changed by other code. /// Note that it's unsafe to use when the dictionary might be changed by other code.
pub fn iter(&self) -> PyDictIterator { pub fn iter(&self) -> PyDictIterator {
let py = self.py();
PyDictIterator { PyDictIterator {
dict: self.to_object(py), dict: self.as_ref(),
pos: 0, pos: 0,
py,
} }
} }
} }
pub struct PyDictIterator<'py> { pub struct PyDictIterator<'py> {
dict: PyObject, dict: &'py PyAny,
pos: isize, pos: isize,
py: Python<'py>,
} }
impl<'py> Iterator for PyDictIterator<'py> { impl<'py> Iterator for PyDictIterator<'py> {
@ -178,7 +175,7 @@ impl<'py> Iterator for PyDictIterator<'py> {
let mut key: *mut ffi::PyObject = std::ptr::null_mut(); let mut key: *mut ffi::PyObject = std::ptr::null_mut();
let mut value: *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 { 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))) Some((py.from_borrowed_ptr(key), py.from_borrowed_ptr(value)))
} else { } else {
None None

View File

@ -101,7 +101,10 @@ impl PySet {
/// Note that it can be unsafe to use when the set might be changed by other code. /// Note that it can be unsafe to use when the set might be changed by other code.
#[cfg(not(Py_LIMITED_API))] #[cfg(not(Py_LIMITED_API))]
pub fn iter(&self) -> PySetIterator { pub fn iter(&self) -> PySetIterator {
self.into_iter() PySetIterator {
set: self.as_ref(),
pos: 0,
}
} }
} }
@ -135,10 +138,7 @@ impl<'a> std::iter::IntoIterator for &'a PySet {
type IntoIter = PySetIterator<'a>; type IntoIter = PySetIterator<'a>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
PySetIterator { self.iter()
set: self.as_ref(),
pos: 0,
}
} }
} }