Returns the list of (key,value) pairs in this dictionary.
(&self, py: Python)
| 115 | |
| 116 | /// Returns the list of (key,value) pairs in this dictionary. |
| 117 | pub fn items(&self, py: Python) -> Vec<(PyObject, PyObject)> { |
| 118 | // Note that we don't provide an iterator because |
| 119 | // PyDict_Next() is unsafe to use when the dictionary might be changed |
| 120 | // by other python code. |
| 121 | let mut vec = Vec::with_capacity(self.len(py)); |
| 122 | let mut pos = 0; |
| 123 | let mut key: *mut ffi::PyObject = ptr::null_mut(); |
| 124 | let mut value: *mut ffi::PyObject = ptr::null_mut(); |
| 125 | unsafe { |
| 126 | while ffi::PyDict_Next(self.0.as_ptr(), &mut pos, &mut key, &mut value) != 0 { |
| 127 | vec.push(( |
| 128 | PyObject::from_borrowed_ptr(py, key), |
| 129 | PyObject::from_borrowed_ptr(py, value), |
| 130 | )); |
| 131 | } |
| 132 | } |
| 133 | vec |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /// Converts a Rust `HashMap` to a Python `dict`. |