Skip to content

Universal mechanism for Iterator slots #3116

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

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion vm/src/builtins/asyncgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::coroutine::{Coro, Variant};
use crate::exceptions::PyBaseExceptionRef;
use crate::frame::FrameRef;
use crate::function::OptionalArg;
use crate::slots::PyIter;
use crate::slots::{IteratorIterable, PyIter};
use crate::vm::VirtualMachine;
use crate::{
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
Expand Down Expand Up @@ -256,6 +256,7 @@ impl PyAsyncGenASend {
}
}

impl IteratorIterable for PyAsyncGenASend {}
impl PyIter for PyAsyncGenASend {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
Expand Down Expand Up @@ -397,6 +398,7 @@ impl PyAsyncGenAThrow {
}
}

impl IteratorIterable for PyAsyncGenAThrow {}
impl PyIter for PyAsyncGenAThrow {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
Expand Down
4 changes: 3 additions & 1 deletion vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use crate::common::lock::{
use crate::function::{ArgIterable, FuncArgs, OptionalArg, OptionalOption};
use crate::sliceable::{PySliceableSequence, PySliceableSequenceMut, SequenceIndex};
use crate::slots::{
AsBuffer, Callable, Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhashable,
AsBuffer, Callable, Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter,
Unhashable,
};
use crate::utils::Either;
use crate::vm::VirtualMachine;
Expand Down Expand Up @@ -747,6 +748,7 @@ impl PyValue for PyByteArrayIterator {

#[pyimpl(with(PyIter))]
impl PyByteArrayIterator {}
impl IteratorIterable for PyByteArrayIterator {}
impl PyIter for PyByteArrayIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let pos = zelf.position.fetch_add(1);
Expand Down
4 changes: 3 additions & 1 deletion vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use crate::byteslike::ArgBytesLike;
use crate::common::hash::PyHash;
use crate::function::{ArgIterable, OptionalArg, OptionalOption};
use crate::slots::{
AsBuffer, Callable, Comparable, Hashable, Iterable, PyComparisonOp, PyIter, SlotConstructor,
AsBuffer, Callable, Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter,
SlotConstructor,
};
use crate::utils::Either;
use crate::vm::VirtualMachine;
Expand Down Expand Up @@ -601,6 +602,7 @@ impl PyValue for PyBytesIterator {

#[pyimpl(with(PyIter))]
impl PyBytesIterator {}
impl IteratorIterable for PyBytesIterator {}
impl PyIter for PyBytesIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let pos = zelf.position.fetch_add(1);
Expand Down
4 changes: 3 additions & 1 deletion vm/src/builtins/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::pytype::PyTypeRef;
use crate::coroutine::{Coro, Variant};
use crate::frame::FrameRef;
use crate::function::OptionalArg;
use crate::slots::PyIter;
use crate::slots::{IteratorIterable, PyIter};
use crate::vm::VirtualMachine;
use crate::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};

Expand Down Expand Up @@ -104,6 +104,7 @@ impl PyCoroutine {
}
}

impl IteratorIterable for PyCoroutine {}
impl PyIter for PyCoroutine {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
Expand Down Expand Up @@ -141,6 +142,7 @@ impl PyCoroutineWrapper {
}
}

impl IteratorIterable for PyCoroutineWrapper {}
impl PyIter for PyCoroutineWrapper {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
Expand Down
6 changes: 5 additions & 1 deletion vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use crate::dictdatatype::{self, DictKey};
use crate::exceptions::PyBaseExceptionRef;
use crate::function::{ArgIterable, FuncArgs, KwArgs, OptionalArg};
use crate::iterator;
use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhashable};
use crate::slots::{
Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter, Unhashable,
};
use crate::vm::{ReprGuard, VirtualMachine};
use crate::{
IdProtocol, IntoPyObject, ItemProtocol, PyArithmaticValue::*, PyAttributes, PyClassDef,
Expand Down Expand Up @@ -731,6 +733,7 @@ macro_rules! dict_iterator {
}
}

impl IteratorIterable for $iter_name {}
impl PyIter for $iter_name {
#[allow(clippy::redundant_closure_call)]
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Expand Down Expand Up @@ -791,6 +794,7 @@ macro_rules! dict_iterator {
}
}

impl IteratorIterable for $reverse_iter_name {}
impl PyIter for $reverse_iter_name {
#[allow(clippy::redundant_closure_call)]
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Expand Down
4 changes: 3 additions & 1 deletion vm/src/builtins/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::iter::{
};
use super::pytype::PyTypeRef;
use crate::function::OptionalArg;
use crate::slots::{PyIter, SlotConstructor};
use crate::slots::{IteratorIterable, PyIter, SlotConstructor};
use crate::vm::VirtualMachine;
use crate::{iterator, ItemProtocol, TypeProtocol};
use crate::{IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
Expand Down Expand Up @@ -56,6 +56,7 @@ impl SlotConstructor for PyEnumerate {
#[pyimpl(with(PyIter, SlotConstructor), flags(BASETYPE))]
impl PyEnumerate {}

impl IteratorIterable for PyEnumerate {}
impl PyIter for PyEnumerate {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let next_obj = iterator::call_next(vm, &zelf.iterator)?;
Expand Down Expand Up @@ -137,6 +138,7 @@ impl PyReverseSequenceIterator {
}
}

impl IteratorIterable for PyReverseSequenceIterator {}
impl PyIter for PyReverseSequenceIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if let Exhausted = zelf.status.load() {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::pytype::PyTypeRef;
use crate::iterator;
use crate::slots::{PyIter, SlotConstructor};
use crate::slots::{IteratorIterable, PyIter, SlotConstructor};
use crate::vm::VirtualMachine;
use crate::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};

Expand Down Expand Up @@ -50,6 +50,7 @@ impl SlotConstructor for PyFilter {
#[pyimpl(with(PyIter, SlotConstructor), flags(BASETYPE))]
impl PyFilter {}

impl IteratorIterable for PyFilter {}
impl PyIter for PyFilter {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let predicate = &zelf.predicate;
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::pytype::PyTypeRef;
use crate::coroutine::{Coro, Variant};
use crate::frame::FrameRef;
use crate::function::OptionalArg;
use crate::slots::PyIter;
use crate::slots::{IteratorIterable, PyIter};
use crate::vm::VirtualMachine;
use crate::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};

Expand Down Expand Up @@ -95,6 +95,7 @@ impl PyGenerator {
}
}

impl IteratorIterable for PyGenerator {}
impl PyIter for PyGenerator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
Expand Down
4 changes: 3 additions & 1 deletion vm/src/builtins/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crossbeam_utils::atomic::AtomicCell;

use super::pytype::PyTypeRef;
use super::{int, PyInt};
use crate::slots::PyIter;
use crate::slots::{IteratorIterable, PyIter};
use crate::vm::VirtualMachine;
use crate::{
function::ArgCallable, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult,
Expand Down Expand Up @@ -92,6 +92,7 @@ impl PySequenceIterator {
}
}

impl IteratorIterable for PySequenceIterator {}
impl PyIter for PySequenceIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if let IterStatus::Exhausted = zelf.status.load() {
Expand Down Expand Up @@ -134,6 +135,7 @@ impl PyCallableIterator {
}
}

impl IteratorIterable for PyCallableIterator {}
impl PyIter for PyCallableIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if let IterStatus::Exhausted = zelf.status.load() {
Expand Down
6 changes: 5 additions & 1 deletion vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use crate::common::lock::{
use crate::function::{ArgIterable, FuncArgs, OptionalArg};
use crate::sequence::{self, SimpleSeq};
use crate::sliceable::{PySliceableSequence, PySliceableSequenceMut, SequenceIndex};
use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhashable};
use crate::slots::{
Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter, Unhashable,
};
use crate::utils::Either;
use crate::vm::{ReprGuard, VirtualMachine};
use crate::{
Expand Down Expand Up @@ -526,6 +528,7 @@ impl PyListIterator {
}
}

impl IteratorIterable for PyListIterator {}
impl PyIter for PyListIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if let Exhausted = zelf.status.load() {
Expand Down Expand Up @@ -599,6 +602,7 @@ impl PyListReverseIterator {
}
}

impl IteratorIterable for PyListReverseIterator {}
impl PyIter for PyListReverseIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if let Exhausted = zelf.status.load() {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::pytype::PyTypeRef;
use crate::function::PosArgs;
use crate::iterator;
use crate::slots::{PyIter, SlotConstructor};
use crate::slots::{IteratorIterable, PyIter, SlotConstructor};
use crate::vm::VirtualMachine;
use crate::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};

Expand Down Expand Up @@ -50,6 +50,7 @@ impl PyMap {
}
}

impl IteratorIterable for PyMap {}
impl PyIter for PyMap {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let next_objs = zelf
Expand Down
5 changes: 4 additions & 1 deletion vm/src/builtins/pystr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use crate::exceptions::IntoPyException;
use crate::format::{FormatSpec, FormatString, FromTemplate};
use crate::function::{ArgIterable, FuncArgs, OptionalArg, OptionalOption};
use crate::sliceable::PySliceableSequence;
use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, SlotConstructor};
use crate::slots::{
Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter, SlotConstructor,
};
use crate::utils::Either;
use crate::VirtualMachine;
use crate::{
Expand Down Expand Up @@ -227,6 +229,7 @@ impl PyStrIterator {
}
}

impl IteratorIterable for PyStrIterator {}
impl PyIter for PyStrIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if let Exhausted = zelf.status.load() {
Expand Down
4 changes: 3 additions & 1 deletion vm/src/builtins/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::pytype::PyTypeRef;
use super::slice::{PySlice, PySliceRef};
use crate::common::hash::PyHash;
use crate::function::{FuncArgs, OptionalArg};
use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter};
use crate::slots::{Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter};
use crate::vm::VirtualMachine;
use crate::{
iterator, IdProtocol, IntoPyRef, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
Expand Down Expand Up @@ -516,6 +516,7 @@ impl PyLongRangeIterator {
}
}

impl IteratorIterable for PyLongRangeIterator {}
impl PyIter for PyLongRangeIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
// TODO: In pathological case (index == usize::MAX) this can wrap around
Expand Down Expand Up @@ -585,6 +586,7 @@ impl PyRangeIterator {
}
}

impl IteratorIterable for PyRangeIterator {}
impl PyIter for PyRangeIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
// TODO: In pathological case (index == usize::MAX) this can wrap around
Expand Down
4 changes: 3 additions & 1 deletion vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::dictdatatype;
use crate::dictdatatype::DictSize;
use crate::function::{ArgIterable, FuncArgs, OptionalArg, PosArgs};
use crate::slots::{
Comparable, Hashable, Iterable, PyComparisonOp, PyIter, SlotConstructor, Unhashable,
Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter, SlotConstructor,
Unhashable,
};
use crate::vm::{ReprGuard, VirtualMachine};
use crate::{
Expand Down Expand Up @@ -864,6 +865,7 @@ impl PySetIterator {
}
}

impl IteratorIterable for PySetIterator {}
impl PyIter for PySetIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
match zelf.status.load() {
Expand Down
5 changes: 4 additions & 1 deletion vm/src/builtins/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use crate::common::hash::PyHash;
use crate::function::OptionalArg;
use crate::sequence::{self, SimpleSeq};
use crate::sliceable::PySliceableSequence;
use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, SlotConstructor};
use crate::slots::{
Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter, SlotConstructor,
};
use crate::utils::Either;
use crate::vm::{ReprGuard, VirtualMachine};
use crate::{
Expand Down Expand Up @@ -378,6 +380,7 @@ impl PyTupleIterator {
}
}

impl IteratorIterable for PyTupleIterator {}
impl PyIter for PyTupleIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if let Exhausted = zelf.status.load() {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/zip.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::pytype::PyTypeRef;
use crate::function::PosArgs;
use crate::iterator;
use crate::slots::{PyIter, SlotConstructor};
use crate::slots::{IteratorIterable, PyIter, SlotConstructor};
use crate::vm::VirtualMachine;
use crate::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};

Expand Down Expand Up @@ -32,6 +32,7 @@ impl SlotConstructor for PyZip {
#[pyimpl(with(PyIter, SlotConstructor), flags(BASETYPE))]
impl PyZip {}

impl IteratorIterable for PyZip {}
impl PyIter for PyZip {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if zelf.iterators.is_empty() {
Expand Down
16 changes: 9 additions & 7 deletions vm/src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ pub trait AsBuffer: PyValue {
#[pyimpl]
pub trait Iterable: PyValue {
#[pyslot]
#[pymethod(name = "__iter__")]
fn tp_iter(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult {
if let Ok(zelf) = zelf.downcast() {
Self::iter(zelf, vm)
Expand All @@ -541,12 +542,11 @@ pub trait Iterable: PyValue {
}
}

#[pymethod(magic)]
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult;
}

#[pyimpl(with(Iterable))]
pub trait PyIter: PyValue {
pub trait PyIter: PyValue + Iterable {
#[pyslot]
fn tp_iternext(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult {
if let Some(zelf) = zelf.downcast_ref() {
Expand All @@ -559,19 +559,21 @@ pub trait PyIter: PyValue {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult;

#[pymethod]
fn __next__(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Self::next(&zelf, vm)
fn __next__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Self::tp_iternext(&zelf, vm)
}
}

pub trait IteratorIterable: PyValue {}

impl<T> Iterable for T
where
T: PyIter,
T: IteratorIterable,
{
fn tp_iter(zelf: PyObjectRef, _vm: &VirtualMachine) -> PyResult {
Ok(zelf)
}
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyResult {
Ok(zelf.into_object())
fn iter(_zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyResult {
unreachable!("tp_iter is implemented");
}
}
Loading