Add reversed iterator type.#2900
Conversation
aa3833b to
ca6ab79
Compare
00445b5 to
fe4c3a5
Compare
| use crate::iterator; | ||
| use crate::slots::PyIter; | ||
| use crate::vm::VirtualMachine; | ||
| use crate::{iterator, ItemProtocol, TypeProtocol}; |
There was a problem hiding this comment.
same level as the next line (Line 13)
|
|
||
| #[pyclass(module = false, name = "reversed")] | ||
| #[derive(Debug)] | ||
| pub struct PyReverseSequenceIterator { |
There was a problem hiding this comment.
is this belong to enuemerate rather than sequence? I feel like this iterator generate (int, object) rather than object due to the placed module.
There was a problem hiding this comment.
I know. I don't know why CPython has them in the same file but it does. So I kept the same structure.
| #[pyclass(module = false, name = "reversed")] | ||
| #[derive(Debug)] | ||
| pub struct PyReverseSequenceIterator { | ||
| pub position: AtomicCell<isize>, |
There was a problem hiding this comment.
is this isize rather than a usize?
There was a problem hiding this comment.
Usually the reverse iterators keep an isize in order to check that its > 0 while decreasing it and indexing the underlying object.
|
|
||
| impl PyIter for PyReverseSequenceIterator { | ||
| fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult { | ||
| let pos = zelf.position.fetch_sub(1); |
There was a problem hiding this comment.
what happens if this is called forever until negatively overflow?
There was a problem hiding this comment.
I'm fixing all these methods in another pr. Currently I've just split these appart for now to make the changes a little smaller in size.
There was a problem hiding this comment.
Will you make another huge PR with the whole diff for a reference? I will also check the final result.
There was a problem hiding this comment.
I could push the changes on this branch if it would make it easier for you.
There was a problem hiding this comment.
I think seperated small PR still have benefits. So another PR would be more convenient. It will be reusable after merging this one.
For this PR, I thought I can compare other parts also, but after checking your answers about it, it doesn't look that's nessessary for now.
PySequenceIteratorhandles forward iteration whilePyReverseSequenceIterator, located inenumerate.rs(not sure why) as in CPython, handles reverse iteration.