From 7b502821ce5f58b471781de09a8a1ae1a0635e52 Mon Sep 17 00:00:00 2001 From: kngwyu Date: Wed, 8 Jan 2020 23:43:26 +0900 Subject: [PATCH] Use &PyAny instead of PyObject in PyDictIterator --- src/types/dict.rs | 9 +++------ src/types/set.rs | 10 +++++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/types/dict.rs b/src/types/dict.rs index 418819d2..74bbd0c4 100644 --- a/src/types/dict.rs +++ b/src/types/dict.rs @@ -154,19 +154,16 @@ impl PyDict { /// /// 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> { @@ -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 diff --git a/src/types/set.rs b/src/types/set.rs index e1257b8b..23c43f0b 100644 --- a/src/types/set.rs +++ b/src/types/set.rs @@ -101,7 +101,10 @@ impl PySet { /// 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() + PySetIterator { + set: self.as_ref(), + pos: 0, + } } } @@ -135,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() } }