Skip to content

Commit

Permalink
Proper List + Queue iterators.
Browse files Browse the repository at this point in the history
We reimplement them, as I can't figure out whether it's feasible to use the
existing native Rust Iter implementations.
  • Loading branch information
Julian committed Jan 10, 2024
1 parent 182dfd9 commit d808005
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,12 @@ impl ListPy {
}

fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<ListIterator>> {
let iter = slf
.inner
.iter()
.map(|k| k.to_owned())
.collect::<Vec<_>>()
.into_iter();
Py::new(slf.py(), ListIterator { inner: iter })
Py::new(
slf.py(),
ListIterator {
inner: slf.inner.clone(),
},
)
}

fn __reversed__(&self) -> ListPy {
Expand Down Expand Up @@ -627,7 +626,7 @@ impl ListPy {

#[pyclass(module = "rpds")]
struct ListIterator {
inner: IntoIter<PyObject>,
inner: ListSync<PyObject>,
}

#[pymethods]
Expand All @@ -637,7 +636,27 @@ impl ListIterator {
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<PyObject> {
slf.inner.next()
let first = slf.inner.first()?.to_owned();
slf.inner = slf.inner.drop_first()?;
Some(first)
}
}

#[pyclass(module = "rpds")]
struct QueueIterator {
inner: QueueSync<PyObject>,
}

#[pymethods]
impl QueueIterator {
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
slf
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<PyObject> {
let first = slf.inner.peek()?.to_owned();
slf.inner = slf.inner.dequeue()?;
Some(first)
}
}

Expand Down Expand Up @@ -704,14 +723,13 @@ impl QueuePy {
.any(|r| r.unwrap_or(true))
}

fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<ListIterator>> {
let iter = slf
.inner
.iter()
.map(|k| k.to_owned())
.collect::<Vec<_>>()
.into_iter();
Py::new(slf.py(), ListIterator { inner: iter })
fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<QueueIterator>> {
Py::new(
slf.py(),
QueueIterator {
inner: slf.inner.clone(),
},
)
}

fn __len__(&self) -> usize {
Expand Down

0 comments on commit d808005

Please sign in to comment.