Skip to content

Add reversed iterator type.#2900

Merged
youknowone merged 2 commits into
RustPython:masterfrom
DimitrisJim:reversed_iter_type
Aug 17, 2021
Merged

Add reversed iterator type.#2900
youknowone merged 2 commits into
RustPython:masterfrom
DimitrisJim:reversed_iter_type

Conversation

@DimitrisJim

Copy link
Copy Markdown
Member

PySequenceIterator handles forward iteration while PyReverseSequenceIterator, located in enumerate.rs (not sure why) as in CPython, handles reverse iteration.

use crate::iterator;
use crate::slots::PyIter;
use crate::vm::VirtualMachine;
use crate::{iterator, ItemProtocol, TypeProtocol};

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.

same level as the next line (Line 13)


#[pyclass(module = false, name = "reversed")]
#[derive(Debug)]
pub struct PyReverseSequenceIterator {

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.

is this belong to enuemerate rather than sequence? I feel like this iterator generate (int, object) rather than object due to the placed module.

@DimitrisJim DimitrisJim Aug 17, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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>,

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.

is this isize rather than a usize?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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);

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.

what happens if this is called forever until negatively overflow?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

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.

Will you make another huge PR with the whole diff for a reference? I will also check the final result.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I could push the changes on this branch if it would make it easier for you.

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.

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.

@youknowone youknowone merged commit f20bb6c into RustPython:master Aug 17, 2021
@DimitrisJim DimitrisJim deleted the reversed_iter_type branch November 7, 2021 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants