Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add arrayiter __reduce__
Co-authored-by: Yaminyam <siontama@gmail.com>
  • Loading branch information
maong0927 and Yaminyam committed Jul 13, 2022
commit 0f24b183121e95e38bf7cfa677607c4dfd2ba610
22 changes: 16 additions & 6 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ mod array {
common::{
atomic::{self, AtomicUsize},
lock::{
PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyRwLock, PyRwLockReadGuard,
PyRwLockWriteGuard,
PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutex, PyRwLock,
PyRwLockReadGuard, PyRwLockWriteGuard,
},
str::wchar_t,
},
vm::{
builtins::{
PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat, PyInt, PyIntRef, PyList,
PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat, PyInt,
PyIntRef, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
},
class_or_notimplemented,
convert::{ToPyObject, ToPyResult, TryFromObject},
Expand Down Expand Up @@ -1260,7 +1260,8 @@ mod array {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(PyArrayIterator {
position: AtomicUsize::new(0),
array: zelf,
array: zelf.clone(),
internal: PyMutex::new(PositionIterInternal::new(zelf.clone(), 0)),
}
.into_pyobject(vm))
}
Expand All @@ -1281,10 +1282,19 @@ mod array {
pub struct PyArrayIterator {
position: AtomicUsize,
array: PyArrayRef,
internal: PyMutex<PositionIterInternal<PyArrayRef>>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you really need both position and internal? Though I didn't look in details, those fields look like duplication.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it because internal contains position because it is PositionIterInternal as its name suggests?

Copy link
Copy Markdown
Member

@youknowone youknowone Jul 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the PositionIterInternal type. it contains position field. By this change, the code have duplicated position fields but most of code uses position and new code uses internal. I don't think it can correctly work

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With comparison with other types' iterator, we really don't need position and array field because PyMutex<PositionIterInternal<PyArrayRef>> will do their roles. Because of __next__ and __setstate__, we need to modify position and array fields. It leads to the need for PyMutex.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we replace position and array fields with PyMutex<PositionIterInternal<PyArrayRef>>, we can implement other methods as usual we did (__next__ and __reduce__)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does arrayiterator require closed field? I thought it only need position.
closed is useful when it include another iterator in it.

}

#[pyimpl(with(IterNext))]
impl PyArrayIterator {}

impl PyArrayIterator {
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef {
self.internal
.lock()
.builtins_iter_reduce(|x| x.clone().into(), vm)
}
}

impl IterNextIterable for PyArrayIterator {}
impl IterNext for PyArrayIterator {
Expand Down