Skip to content

Commit 3500fb6

Browse files
committed
Use slice for list iteration
1 parent 814c5f2 commit 3500fb6

2 files changed

Lines changed: 8 additions & 35 deletions

File tree

src/encode.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,16 @@ impl<'p> Serialize for SerializePyObject {
9797
if unlikely!(self.recursion == RECURSION_LIMIT) {
9898
err!("Recursion limit reached")
9999
}
100+
let slice: &[*mut pyo3::ffi::PyObject] = unsafe {
101+
std::slice::from_raw_parts(
102+
(*(self.ptr as *mut pyo3::ffi::PyListObject)).ob_item,
103+
ffi!(PyList_GET_SIZE(self.ptr)) as usize,
104+
)
105+
};
100106
let mut seq = serializer.serialize_seq(None).unwrap();
101-
for elem in PyListIterator::new(self.ptr) {
107+
for &elem in slice {
102108
seq.serialize_element(&SerializePyObject {
103-
ptr: elem.as_ptr(),
109+
ptr: elem,
104110
default: self.default,
105111
opts: self.opts,
106112
default_calls: self.default_calls,

src/iter.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,6 @@
22

33
use std::ptr::NonNull;
44

5-
pub struct PyListIterator {
6-
list: *mut pyo3::ffi::PyObject,
7-
len: isize,
8-
idx: isize,
9-
}
10-
11-
impl PyListIterator {
12-
pub fn new(list: *mut pyo3::ffi::PyObject) -> Self {
13-
PyListIterator {
14-
list: list,
15-
len: ffi!(PyList_GET_SIZE(list)),
16-
idx: 0,
17-
}
18-
}
19-
}
20-
21-
impl Iterator for PyListIterator {
22-
type Item = NonNull<pyo3::ffi::PyObject>;
23-
24-
#[inline]
25-
fn next(&mut self) -> Option<NonNull<pyo3::ffi::PyObject>> {
26-
if self.len == self.idx {
27-
None
28-
} else {
29-
let item = unsafe {
30-
NonNull::new_unchecked(ffi!(PyList_GET_ITEM(self.list, self.idx as isize)))
31-
};
32-
self.idx += 1;
33-
Some(item)
34-
}
35-
}
36-
}
37-
385
pub struct PyTupleIterator {
396
list: *mut pyo3::ffi::PyObject,
407
len: isize,

0 commit comments

Comments
 (0)