-
Notifications
You must be signed in to change notification settings - Fork 1.4k
add arrayiter.__reduce__ #3868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add arrayiter.__reduce__ #3868
Conversation
stdlib/src/array.rs
Outdated
|
|
||
| impl Iterable for PyArray { | ||
| fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult { | ||
| let zelf_ = zelf.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| let zelf_ = zelf.clone(); |
stdlib/src/array.rs
Outdated
|
|
||
| impl Iterable for PyArray { | ||
| fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult { | ||
| let zelf_ = zelf.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ prefix usually means unused variable in rust code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _ suffix usually indicates that you're dodging the use of a reserved keyword. From the Rust style guide:
When a name is forbidden because it is a reserved word (e.g.,
crate), use a trailing underscore to make the name legal (e.g.,crate_), or use raw identifiers if possible.
stdlib/src/array.rs
Outdated
| let zelf_ = zelf.clone(); | ||
| Ok(PyArrayIter { | ||
| position: AtomicUsize::new(0), | ||
| array: zelf, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| array: zelf, | |
| array: zelf.clone(), |
change here to allow to use zelf later.
stdlib/src/array.rs
Outdated
| Ok(PyArrayIter { | ||
| position: AtomicUsize::new(0), | ||
| array: zelf, | ||
| internal: PyMutex::new(PositionIterInternal::new(zelf_, 0)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| internal: PyMutex::new(PositionIterInternal::new(zelf_, 0)), | |
| internal: PyMutex::new(PositionIterInternal::new(zelf.clone(), 0)), |
Is PositionIterInternal necessary for implementing __reduce__?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, this is a good point. internal is doing nothing here. it must be position.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation was implemented by referring to the implementation of list iter.
Is there any reason to use zelf as it is instead of zelf.clone in list iter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @Snowapril got confused here. because zelf is already moved in upper line, it can't be cloned here.
|
Please do not use git merge. Would you try to rebase it? |
| pub struct PyArrayIter { | ||
| position: AtomicUsize, | ||
| array: PyArrayRef, | ||
| internal: PyMutex<PositionIterInternal<PyArrayRef>>, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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__)
There was a problem hiding this comment.
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.
The following commands should get you started: |
|
because people sometimes use this is same to Though I never use |
|
I'll use rebase from now on |
Co-authored-by: Yaminyam <siontama@gmail.com>
Co-authored-by: Yaminyam <siontama@gmail.com>
776a977 to
821f3e7
Compare
|
According to the CI, there are a few more tests to address. You can run the following command to take a closer look: In addition, you can run |
Co-authored-by: Snowapril <sinjihng@gmail.com>
youknowone
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for contributing!
No description provided.