diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index aee4ab65ae..762573ba67 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -61,8 +61,8 @@ mod array { SliceableSequenceOp, }, types::{ - AsBuffer, AsMapping, AsSequence, Comparable, Constructor, IterNext, - IterNextIterable, Iterable, PyComparisonOp, Representable, + AsBuffer, AsMapping, AsSequence, Comparable, Constructor, IterNext, Iterable, + PyComparisonOp, Representable, SelfIter, }, AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, }, @@ -1405,7 +1405,7 @@ mod array { internal: PyMutex>, } - #[pyclass(with(IterNext), flags(HAS_DICT))] + #[pyclass(with(IterNext, Iterable), flags(HAS_DICT))] impl PyArrayIter { #[pymethod(magic)] fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { @@ -1422,7 +1422,7 @@ mod array { } } - impl IterNextIterable for PyArrayIter {} + impl SelfIter for PyArrayIter {} impl IterNext for PyArrayIter { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|array, pos| { diff --git a/stdlib/src/csv.rs b/stdlib/src/csv.rs index c49dec0798..bee3fd5faa 100644 --- a/stdlib/src/csv.rs +++ b/stdlib/src/csv.rs @@ -8,7 +8,7 @@ mod _csv { function::{ArgIterable, ArgumentError, FromArgs, FuncArgs}, match_class, protocol::{PyIter, PyIterReturn}, - types::{IterNext, IterNextIterable}, + types::{IterNext, Iterable, SelfIter}, AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromObject, VirtualMachine, }; use itertools::{self, Itertools}; @@ -166,9 +166,9 @@ mod _csv { } } - #[pyclass(with(IterNext))] + #[pyclass(with(IterNext, Iterable))] impl Reader {} - impl IterNextIterable for Reader {} + impl SelfIter for Reader {} impl IterNext for Reader { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let string = match zelf.iter.next(vm)? { diff --git a/stdlib/src/pystruct.rs b/stdlib/src/pystruct.rs index 96c6d9dd2c..2d83e9570d 100644 --- a/stdlib/src/pystruct.rs +++ b/stdlib/src/pystruct.rs @@ -15,7 +15,7 @@ pub(crate) mod _struct { function::{ArgBytesLike, ArgMemoryBuffer, PosArgs}, match_class, protocol::PyIterReturn, - types::{Constructor, IterNext, IterNextIterable}, + types::{Constructor, IterNext, Iterable, SelfIter}, AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromObject, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; @@ -194,14 +194,14 @@ pub(crate) mod _struct { } } - #[pyclass(with(IterNext))] + #[pyclass(with(IterNext, Iterable))] impl UnpackIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { self.buffer.len().saturating_sub(self.offset.load()) / self.format_spec.size } } - impl IterNextIterable for UnpackIterator {} + impl SelfIter for UnpackIterator {} impl IterNext for UnpackIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let size = zelf.format_spec.size; diff --git a/stdlib/src/sqlite.rs b/stdlib/src/sqlite.rs index addc781ac2..32ea0d0cd5 100644 --- a/stdlib/src/sqlite.rs +++ b/stdlib/src/sqlite.rs @@ -62,8 +62,8 @@ mod _sqlite { protocol::{PyBuffer, PyIterReturn, PyMappingMethods, PySequence, PySequenceMethods}, sliceable::{SaturatedSliceIter, SliceableSequenceOp}, types::{ - AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, IterNext, - IterNextIterable, Iterable, PyComparisonOp, + AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, IterNext, Iterable, + PyComparisonOp, SelfIter, }, utils::ToCString, AsObject, Py, PyAtomicRef, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, @@ -1377,7 +1377,7 @@ mod _sqlite { statement: Option>, } - #[pyclass(with(Constructor, IterNext), flags(BASETYPE))] + #[pyclass(with(Constructor, IterNext, Iterable), flags(BASETYPE))] impl Cursor { fn new( connection: PyRef, @@ -1708,7 +1708,7 @@ mod _sqlite { } } - impl IterNextIterable for Cursor {} + impl SelfIter for Cursor {} impl IterNext for Cursor { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let mut inner = zelf.inner(vm)?; diff --git a/vm/src/builtins/asyncgenerator.rs b/vm/src/builtins/asyncgenerator.rs index 287ab920c7..d58744da0a 100644 --- a/vm/src/builtins/asyncgenerator.rs +++ b/vm/src/builtins/asyncgenerator.rs @@ -6,7 +6,7 @@ use crate::{ frame::FrameRef, function::OptionalArg, protocol::PyIterReturn, - types::{Constructor, IterNext, IterNextIterable, Representable, Unconstructible}, + types::{Constructor, IterNext, Iterable, Representable, SelfIter, Unconstructible}, AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, }; @@ -195,7 +195,7 @@ impl PyPayload for PyAsyncGenASend { } } -#[pyclass(with(IterNext))] +#[pyclass(with(IterNext, Iterable))] impl PyAsyncGenASend { #[pymethod(name = "__await__")] fn r#await(zelf: PyRef, _vm: &VirtualMachine) -> PyRef { @@ -268,7 +268,7 @@ impl PyAsyncGenASend { } } -impl IterNextIterable for PyAsyncGenASend {} +impl SelfIter for PyAsyncGenASend {} impl IterNext for PyAsyncGenASend { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { PyIterReturn::from_pyresult(zelf.send(vm.ctx.none(), vm), vm) @@ -290,7 +290,7 @@ impl PyPayload for PyAsyncGenAThrow { } } -#[pyclass(with(IterNext))] +#[pyclass(with(IterNext, Iterable))] impl PyAsyncGenAThrow { #[pymethod(name = "__await__")] fn r#await(zelf: PyRef, _vm: &VirtualMachine) -> PyRef { @@ -414,7 +414,7 @@ impl PyAsyncGenAThrow { } } -impl IterNextIterable for PyAsyncGenAThrow {} +impl SelfIter for PyAsyncGenAThrow {} impl IterNext for PyAsyncGenAThrow { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { PyIterReturn::from_pyresult(zelf.send(vm.ctx.none(), vm), vm) diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index 87757fae36..f1279389e4 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -31,7 +31,7 @@ use crate::{ sliceable::{SequenceIndex, SliceableSequenceMutOp, SliceableSequenceOp}, types::{ AsBuffer, AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Initializer, - IterNext, IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible, + IterNext, Iterable, PyComparisonOp, Representable, SelfIter, Unconstructible, }, AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine, @@ -891,7 +891,7 @@ impl PyPayload for PyByteArrayIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PyByteArrayIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { @@ -914,7 +914,7 @@ impl PyByteArrayIterator { impl Unconstructible for PyByteArrayIterator {} -impl IterNextIterable for PyByteArrayIterator {} +impl SelfIter for PyByteArrayIterator {} impl IterNext for PyByteArrayIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|bytearray, pos| { diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 18ea0d01ca..b27a8a2df3 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -21,7 +21,7 @@ use crate::{ sliceable::{SequenceIndex, SliceableSequenceOp}, types::{ AsBuffer, AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Hashable, - IterNext, IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible, + IterNext, Iterable, PyComparisonOp, Representable, SelfIter, Unconstructible, }, AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromBorrowedObject, TryFromObject, VirtualMachine, @@ -688,7 +688,7 @@ impl PyPayload for PyBytesIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PyBytesIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { @@ -711,7 +711,7 @@ impl PyBytesIterator { } impl Unconstructible for PyBytesIterator {} -impl IterNextIterable for PyBytesIterator {} +impl SelfIter for PyBytesIterator {} impl IterNext for PyBytesIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|bytes, pos| { diff --git a/vm/src/builtins/coroutine.rs b/vm/src/builtins/coroutine.rs index 440ff46b50..2454e27e2c 100644 --- a/vm/src/builtins/coroutine.rs +++ b/vm/src/builtins/coroutine.rs @@ -5,7 +5,7 @@ use crate::{ frame::FrameRef, function::OptionalArg, protocol::PyIterReturn, - types::{Constructor, IterNext, IterNextIterable, Representable, Unconstructible}, + types::{Constructor, IterNext, Iterable, Representable, SelfIter, Unconstructible}, AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, }; @@ -108,7 +108,7 @@ impl Representable for PyCoroutine { } } -impl IterNextIterable for PyCoroutine {} +impl SelfIter for PyCoroutine {} impl IterNext for PyCoroutine { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { Self::send(zelf, vm.ctx.none(), vm) @@ -128,7 +128,7 @@ impl PyPayload for PyCoroutineWrapper { } } -#[pyclass(with(IterNext))] +#[pyclass(with(IterNext, Iterable))] impl PyCoroutineWrapper { #[pymethod] fn send(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult { @@ -147,7 +147,7 @@ impl PyCoroutineWrapper { } } -impl IterNextIterable for PyCoroutineWrapper {} +impl SelfIter for PyCoroutineWrapper {} impl IterNext for PyCoroutineWrapper { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { Self::send(zelf, vm.ctx.none(), vm) diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index b2a191fd93..a164fc11b6 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -20,7 +20,7 @@ use crate::{ recursion::ReprGuard, types::{ AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Initializer, IterNext, - IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible, + Iterable, PyComparisonOp, Representable, SelfIter, Unconstructible, }, vm::VirtualMachine, AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult, @@ -839,7 +839,7 @@ macro_rules! dict_view { } } - #[pyclass(with(Constructor, IterNext))] + #[pyclass(with(Constructor, IterNext, Iterable))] impl $iter_name { fn new(dict: PyDictRef) -> Self { $iter_name { @@ -870,7 +870,7 @@ macro_rules! dict_view { } impl Unconstructible for $iter_name {} - impl IterNextIterable for $iter_name {} + impl SelfIter for $iter_name {} impl IterNext for $iter_name { #[allow(clippy::redundant_closure_call)] fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { @@ -912,7 +912,7 @@ macro_rules! dict_view { } } - #[pyclass(with(Constructor, IterNext))] + #[pyclass(with(Constructor, IterNext, Iterable))] impl $reverse_iter_name { fn new(dict: PyDictRef) -> Self { let size = dict.size(); @@ -948,7 +948,7 @@ macro_rules! dict_view { } impl Unconstructible for $reverse_iter_name {} - impl IterNextIterable for $reverse_iter_name {} + impl SelfIter for $reverse_iter_name {} impl IterNext for $reverse_iter_name { #[allow(clippy::redundant_closure_call)] fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { diff --git a/vm/src/builtins/enumerate.rs b/vm/src/builtins/enumerate.rs index c1582f08d5..00a3215ad6 100644 --- a/vm/src/builtins/enumerate.rs +++ b/vm/src/builtins/enumerate.rs @@ -7,7 +7,7 @@ use crate::{ convert::ToPyObject, function::OptionalArg, protocol::{PyIter, PyIterReturn}, - types::{Constructor, IterNext, IterNextIterable}, + types::{Constructor, IterNext, Iterable, SelfIter}, AsObject, Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine, }; use num_bigint::BigInt; @@ -52,7 +52,7 @@ impl Constructor for PyEnumerate { } } -#[pyclass(with(Py, IterNext, Constructor), flags(BASETYPE))] +#[pyclass(with(Py, IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyEnumerate { #[pyclassmethod(magic)] fn class_getitem(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias { @@ -71,7 +71,7 @@ impl Py { } } -impl IterNextIterable for PyEnumerate {} +impl SelfIter for PyEnumerate {} impl IterNext for PyEnumerate { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let next_obj = match zelf.iterator.next(vm)? { @@ -97,7 +97,7 @@ impl PyPayload for PyReverseSequenceIterator { } } -#[pyclass(with(IterNext))] +#[pyclass(with(IterNext, Iterable))] impl PyReverseSequenceIterator { pub fn new(obj: PyObjectRef, len: usize) -> Self { let position = len.saturating_sub(1); @@ -130,7 +130,7 @@ impl PyReverseSequenceIterator { } } -impl IterNextIterable for PyReverseSequenceIterator {} +impl SelfIter for PyReverseSequenceIterator {} impl IterNext for PyReverseSequenceIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal diff --git a/vm/src/builtins/filter.rs b/vm/src/builtins/filter.rs index 660be88ea8..3b33ff766f 100644 --- a/vm/src/builtins/filter.rs +++ b/vm/src/builtins/filter.rs @@ -2,7 +2,7 @@ use super::{PyType, PyTypeRef}; use crate::{ class::PyClassImpl, protocol::{PyIter, PyIterReturn}, - types::{Constructor, IterNext, IterNextIterable}, + types::{Constructor, IterNext, Iterable, SelfIter}, Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine, }; @@ -32,7 +32,7 @@ impl Constructor for PyFilter { } } -#[pyclass(with(IterNext, Constructor), flags(BASETYPE))] +#[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyFilter { #[pymethod(magic)] fn reduce(&self, vm: &VirtualMachine) -> (PyTypeRef, (PyObjectRef, PyIter)) { @@ -43,7 +43,7 @@ impl PyFilter { } } -impl IterNextIterable for PyFilter {} +impl SelfIter for PyFilter {} impl IterNext for PyFilter { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let predicate = &zelf.predicate; diff --git a/vm/src/builtins/generator.rs b/vm/src/builtins/generator.rs index 745c33aad9..eceac5ba93 100644 --- a/vm/src/builtins/generator.rs +++ b/vm/src/builtins/generator.rs @@ -9,7 +9,7 @@ use crate::{ frame::FrameRef, function::OptionalArg, protocol::PyIterReturn, - types::{Constructor, IterNext, IterNextIterable, Representable, Unconstructible}, + types::{Constructor, IterNext, Iterable, Representable, SelfIter, Unconstructible}, AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, }; @@ -25,7 +25,7 @@ impl PyPayload for PyGenerator { } } -#[pyclass(with(Py, Constructor, IterNext))] +#[pyclass(with(Py, Constructor, IterNext, Iterable))] impl PyGenerator { pub fn as_coro(&self) -> &Coro { &self.inner @@ -104,7 +104,7 @@ impl Representable for PyGenerator { } } -impl IterNextIterable for PyGenerator {} +impl SelfIter for PyGenerator {} impl IterNext for PyGenerator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.send(vm.ctx.none(), vm) diff --git a/vm/src/builtins/iter.rs b/vm/src/builtins/iter.rs index 6f8a01e057..2fed23a5c4 100644 --- a/vm/src/builtins/iter.rs +++ b/vm/src/builtins/iter.rs @@ -8,7 +8,7 @@ use crate::{ function::ArgCallable, object::{Traverse, TraverseFn}, protocol::{PyIterReturn, PySequence, PySequenceMethods}, - types::{IterNext, IterNextIterable}, + types::{IterNext, Iterable, SelfIter}, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine, }; use rustpython_common::{ @@ -189,7 +189,7 @@ impl PyPayload for PySequenceIterator { } } -#[pyclass(with(IterNext))] +#[pyclass(with(IterNext, Iterable))] impl PySequenceIterator { pub fn new(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { let seq = PySequence::try_protocol(&obj, vm)?; @@ -226,7 +226,7 @@ impl PySequenceIterator { } } -impl IterNextIterable for PySequenceIterator {} +impl SelfIter for PySequenceIterator {} impl IterNext for PySequenceIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|obj, pos| { @@ -252,7 +252,7 @@ impl PyPayload for PyCallableIterator { } } -#[pyclass(with(IterNext))] +#[pyclass(with(IterNext, Iterable))] impl PyCallableIterator { pub fn new(callable: ArgCallable, sentinel: PyObjectRef) -> Self { Self { @@ -262,7 +262,7 @@ impl PyCallableIterator { } } -impl IterNextIterable for PyCallableIterator {} +impl SelfIter for PyCallableIterator {} impl IterNext for PyCallableIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let status = zelf.status.upgradable_read(); diff --git a/vm/src/builtins/list.rs b/vm/src/builtins/list.rs index 581c0fc9e5..03503a0cea 100644 --- a/vm/src/builtins/list.rs +++ b/vm/src/builtins/list.rs @@ -13,8 +13,8 @@ use crate::{ sequence::{MutObjectSequenceOp, OptionalRangeArgs, SequenceExt, SequenceMutExt}, sliceable::{SequenceIndex, SliceableSequenceMutOp, SliceableSequenceOp}, types::{ - AsMapping, AsSequence, Comparable, Constructor, Initializer, IterNext, IterNextIterable, - Iterable, PyComparisonOp, Representable, Unconstructible, + AsMapping, AsSequence, Comparable, Constructor, Initializer, IterNext, Iterable, + PyComparisonOp, Representable, SelfIter, Unconstructible, }, utils::collection_repr, vm::VirtualMachine, @@ -543,7 +543,7 @@ impl PyPayload for PyListIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PyListIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { @@ -566,7 +566,7 @@ impl PyListIterator { } impl Unconstructible for PyListIterator {} -impl IterNextIterable for PyListIterator {} +impl SelfIter for PyListIterator {} impl IterNext for PyListIterator { fn next(zelf: &Py, _vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|list, pos| { @@ -588,7 +588,7 @@ impl PyPayload for PyListReverseIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PyListReverseIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { @@ -611,7 +611,7 @@ impl PyListReverseIterator { } impl Unconstructible for PyListReverseIterator {} -impl IterNextIterable for PyListReverseIterator {} +impl SelfIter for PyListReverseIterator {} impl IterNext for PyListReverseIterator { fn next(zelf: &Py, _vm: &VirtualMachine) -> PyResult { zelf.internal.lock().rev_next(|list, pos| { diff --git a/vm/src/builtins/map.rs b/vm/src/builtins/map.rs index 6fb842f413..44bacf587e 100644 --- a/vm/src/builtins/map.rs +++ b/vm/src/builtins/map.rs @@ -4,7 +4,7 @@ use crate::{ class::PyClassImpl, function::PosArgs, protocol::{PyIter, PyIterReturn}, - types::{Constructor, IterNext, IterNextIterable}, + types::{Constructor, IterNext, Iterable, SelfIter}, Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine, }; @@ -32,7 +32,7 @@ impl Constructor for PyMap { } } -#[pyclass(with(IterNext, Constructor), flags(BASETYPE))] +#[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyMap { #[pymethod(magic)] fn length_hint(&self, vm: &VirtualMachine) -> PyResult { @@ -51,7 +51,7 @@ impl PyMap { } } -impl IterNextIterable for PyMap {} +impl SelfIter for PyMap {} impl IterNext for PyMap { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let mut next_objs = Vec::new(); diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index 312364a744..2c436ca316 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -21,8 +21,8 @@ use crate::{ }, sliceable::SequenceIndexOp, types::{ - AsBuffer, AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, - IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible, + AsBuffer, AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, Iterable, + PyComparisonOp, Representable, SelfIter, Unconstructible, }, AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromBorrowedObject, TryFromObject, VirtualMachine, @@ -1137,7 +1137,7 @@ impl PyPayload for PyMemoryViewIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PyMemoryViewIterator { #[pymethod(magic)] fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef { @@ -1148,7 +1148,7 @@ impl PyMemoryViewIterator { } impl Unconstructible for PyMemoryViewIterator {} -impl IterNextIterable for PyMemoryViewIterator {} +impl SelfIter for PyMemoryViewIterator {} impl IterNext for PyMemoryViewIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|mv, pos| { diff --git a/vm/src/builtins/range.rs b/vm/src/builtins/range.rs index bc15a09093..276c94c1d0 100644 --- a/vm/src/builtins/range.rs +++ b/vm/src/builtins/range.rs @@ -8,8 +8,8 @@ use crate::{ function::{ArgIndex, FuncArgs, OptionalArg, PyComparisonValue}, protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods}, types::{ - AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable, - Iterable, PyComparisonOp, Representable, Unconstructible, + AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, Iterable, + PyComparisonOp, Representable, SelfIter, Unconstructible, }, AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine, @@ -537,7 +537,7 @@ impl PyPayload for PyLongRangeIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PyLongRangeIterator { #[pymethod(magic)] fn length_hint(&self) -> BigInt { @@ -568,7 +568,7 @@ impl PyLongRangeIterator { } impl Unconstructible for PyLongRangeIterator {} -impl IterNextIterable for PyLongRangeIterator {} +impl SelfIter for PyLongRangeIterator {} impl IterNext for PyLongRangeIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { // TODO: In pathological case (index == usize::MAX) this can wrap around @@ -602,7 +602,7 @@ impl PyPayload for PyRangeIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PyRangeIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { @@ -634,7 +634,7 @@ impl PyRangeIterator { } impl Unconstructible for PyRangeIterator {} -impl IterNextIterable for PyRangeIterator {} +impl SelfIter for PyRangeIterator {} impl IterNext for PyRangeIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { // TODO: In pathological case (index == usize::MAX) this can wrap around diff --git a/vm/src/builtins/set.rs b/vm/src/builtins/set.rs index 003b0496ed..29ead02915 100644 --- a/vm/src/builtins/set.rs +++ b/vm/src/builtins/set.rs @@ -16,8 +16,8 @@ use crate::{ recursion::ReprGuard, types::AsNumber, types::{ - AsSequence, Comparable, Constructor, Hashable, Initializer, IterNext, IterNextIterable, - Iterable, PyComparisonOp, Representable, Unconstructible, + AsSequence, Comparable, Constructor, Hashable, Initializer, IterNext, Iterable, + PyComparisonOp, Representable, SelfIter, Unconstructible, }, utils::collection_repr, vm::VirtualMachine, @@ -1232,7 +1232,7 @@ impl PyPayload for PySetIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PySetIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { @@ -1257,7 +1257,7 @@ impl PySetIterator { } impl Unconstructible for PySetIterator {} -impl IterNextIterable for PySetIterator {} +impl SelfIter for PySetIterator {} impl IterNext for PySetIterator { fn next(zelf: &crate::Py, vm: &VirtualMachine) -> PyResult { let mut internal = zelf.internal.lock(); diff --git a/vm/src/builtins/str.rs b/vm/src/builtins/str.rs index 1e29c8d22a..bdb32981de 100644 --- a/vm/src/builtins/str.rs +++ b/vm/src/builtins/str.rs @@ -20,8 +20,8 @@ use crate::{ sequence::SequenceExt, sliceable::{SequenceIndex, SliceableSequenceOp}, types::{ - AsMapping, AsNumber, AsSequence, Comparable, Constructor, Hashable, IterNext, - IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible, + AsMapping, AsNumber, AsSequence, Comparable, Constructor, Hashable, IterNext, Iterable, + PyComparisonOp, Representable, SelfIter, Unconstructible, }, AsObject, Context, Py, PyExact, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult, TryFromBorrowedObject, VirtualMachine, @@ -206,7 +206,7 @@ impl PyPayload for PyStrIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PyStrIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { @@ -232,7 +232,7 @@ impl PyStrIterator { } impl Unconstructible for PyStrIterator {} -impl IterNextIterable for PyStrIterator {} +impl SelfIter for PyStrIterator {} impl IterNext for PyStrIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let mut internal = zelf.internal.lock(); diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index 59133b0876..63cd3e4335 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -12,8 +12,8 @@ use crate::{ sequence::{OptionalRangeArgs, SequenceExt}, sliceable::{SequenceIndex, SliceableSequenceOp}, types::{ - AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable, - Iterable, PyComparisonOp, Representable, Unconstructible, + AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, Iterable, + PyComparisonOp, Representable, SelfIter, Unconstructible, }, utils::collection_repr, vm::VirtualMachine, @@ -434,7 +434,7 @@ impl PyPayload for PyTupleIterator { } } -#[pyclass(with(Constructor, IterNext))] +#[pyclass(with(Constructor, IterNext, Iterable))] impl PyTupleIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { @@ -457,7 +457,7 @@ impl PyTupleIterator { } impl Unconstructible for PyTupleIterator {} -impl IterNextIterable for PyTupleIterator {} +impl SelfIter for PyTupleIterator {} impl IterNext for PyTupleIterator { fn next(zelf: &Py, _vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|tuple, pos| { diff --git a/vm/src/builtins/zip.rs b/vm/src/builtins/zip.rs index d8b28e8eb7..56c88f14c6 100644 --- a/vm/src/builtins/zip.rs +++ b/vm/src/builtins/zip.rs @@ -4,7 +4,7 @@ use crate::{ class::PyClassImpl, function::{ArgIntoBool, OptionalArg, PosArgs}, protocol::{PyIter, PyIterReturn}, - types::{Constructor, IterNext, IterNextIterable}, + types::{Constructor, IterNext, Iterable, SelfIter}, AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine, }; use rustpython_common::atomic::{self, PyAtomic, Radium}; @@ -41,7 +41,7 @@ impl Constructor for PyZip { } } -#[pyclass(with(IterNext, Constructor), flags(BASETYPE))] +#[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyZip { #[pymethod(magic)] fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { @@ -68,7 +68,7 @@ impl PyZip { } } -impl IterNextIterable for PyZip {} +impl SelfIter for PyZip {} impl IterNext for PyZip { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { if zelf.iterators.is_empty() { diff --git a/vm/src/stdlib/collections.rs b/vm/src/stdlib/collections.rs index 5935b124f9..e416792d05 100644 --- a/vm/src/stdlib/collections.rs +++ b/vm/src/stdlib/collections.rs @@ -16,8 +16,8 @@ mod _collections { sequence::{MutObjectSequenceOp, OptionalRangeArgs}, sliceable::SequenceIndexOp, types::{ - AsSequence, Comparable, Constructor, Initializer, IterNext, IterNextIterable, Iterable, - PyComparisonOp, Representable, + AsSequence, Comparable, Constructor, Initializer, IterNext, Iterable, PyComparisonOp, + Representable, SelfIter, }, utils::collection_repr, AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, @@ -623,7 +623,7 @@ mod _collections { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyDequeIterator { pub(crate) fn new(deque: PyDequeRef) -> Self { PyDequeIterator { @@ -654,7 +654,7 @@ mod _collections { } } - impl IterNextIterable for PyDequeIterator {} + impl SelfIter for PyDequeIterator {} impl IterNext for PyDequeIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|deque, pos| { @@ -696,7 +696,7 @@ mod _collections { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyReverseDequeIterator { #[pymethod(magic)] fn length_hint(&self) -> usize { @@ -720,7 +720,7 @@ mod _collections { } } - impl IterNextIterable for PyReverseDequeIterator {} + impl SelfIter for PyReverseDequeIterator {} impl IterNext for PyReverseDequeIterator { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|deque, pos| { diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 3c98c146f3..2a9d224460 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -382,7 +382,7 @@ mod _io { #[derive(Debug, PyPayload)] pub struct _IOBase; - #[pyclass(with(IterNext, Destructor), flags(BASETYPE, HAS_DICT))] + #[pyclass(with(IterNext, Iterable, Destructor), flags(BASETYPE, HAS_DICT))] impl _IOBase { #[pymethod] fn seek( diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 66b4eb50d2..cc5cd35df7 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -16,7 +16,7 @@ mod decl { identifier, protocol::{PyIter, PyIterReturn, PyNumber}, stdlib::sys, - types::{Constructor, IterNext, IterNextIterable, Representable}, + types::{Constructor, IterNext, Iterable, Representable, SelfIter}, AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, PyWeakRef, TryFromObject, VirtualMachine, }; @@ -32,7 +32,7 @@ mod decl { active: PyRwLock>, } - #[pyclass(with(IterNext), flags(BASETYPE, HAS_DICT))] + #[pyclass(with(IterNext, Iterable), flags(BASETYPE, HAS_DICT))] impl PyItertoolsChain { #[pyslot] fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { @@ -110,7 +110,7 @@ mod decl { Ok(()) } } - impl IterNextIterable for PyItertoolsChain {} + impl SelfIter for PyItertoolsChain {} impl IterNext for PyItertoolsChain { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let Some(source) = zelf.source.read().clone() else { @@ -189,7 +189,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] + #[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyItertoolsCompress { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyIter, PyIter)) { @@ -200,7 +200,7 @@ mod decl { } } - impl IterNextIterable for PyItertoolsCompress {} + impl SelfIter for PyItertoolsCompress {} impl IterNext for PyItertoolsCompress { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { loop { @@ -249,7 +249,7 @@ mod decl { return Err(vm.new_type_error("a number is required".to_owned())); } - PyItertoolsCount { + Self { cur: PyRwLock::new(start), step, } @@ -258,7 +258,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor, Representable))] + #[pyclass(with(IterNext, Iterable, Constructor, Representable))] impl PyItertoolsCount { // TODO: Implement this // if (lz->cnt == PY_SSIZE_T_MAX) @@ -268,7 +268,7 @@ mod decl { (zelf.class().to_owned(), (zelf.cur.read().clone(),)) } } - impl IterNextIterable for PyItertoolsCount {} + impl SelfIter for PyItertoolsCount {} impl IterNext for PyItertoolsCount { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let mut cur = zelf.cur.write(); @@ -314,9 +314,9 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] + #[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyItertoolsCycle {} - impl IterNextIterable for PyItertoolsCycle {} + impl SelfIter for PyItertoolsCycle {} impl IterNext for PyItertoolsCycle { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let item = if let PyIterReturn::Return(item) = zelf.iter.next(vm)? { @@ -378,7 +378,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor, Representable), flags(BASETYPE))] + #[pyclass(with(IterNext, Iterable, Constructor, Representable), flags(BASETYPE))] impl PyItertoolsRepeat { #[pymethod(magic)] fn length_hint(&self, vm: &VirtualMachine) -> PyResult { @@ -400,7 +400,7 @@ mod decl { } } - impl IterNextIterable for PyItertoolsRepeat {} + impl SelfIter for PyItertoolsRepeat {} impl IterNext for PyItertoolsRepeat { fn next(zelf: &Py, _vm: &VirtualMachine) -> PyResult { if let Some(ref times) = zelf.times { @@ -456,7 +456,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] + #[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyItertoolsStarmap { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter)) { @@ -466,7 +466,7 @@ mod decl { ) } } - impl IterNextIterable for PyItertoolsStarmap {} + impl SelfIter for PyItertoolsStarmap {} impl IterNext for PyItertoolsStarmap { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let obj = zelf.iterable.next(vm)?; @@ -519,7 +519,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] + #[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyItertoolsTakewhile { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), u32) { @@ -537,7 +537,7 @@ mod decl { Ok(()) } } - impl IterNextIterable for PyItertoolsTakewhile {} + impl SelfIter for PyItertoolsTakewhile {} impl IterNext for PyItertoolsTakewhile { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { if zelf.stop_flag.load() { @@ -600,7 +600,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] + #[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyItertoolsDropwhile { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), u32) { @@ -618,7 +618,7 @@ mod decl { Ok(()) } } - impl IterNextIterable for PyItertoolsDropwhile {} + impl SelfIter for PyItertoolsDropwhile {} impl IterNext for PyItertoolsDropwhile { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let predicate = &zelf.predicate; @@ -719,7 +719,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyItertoolsGroupBy { pub(super) fn advance( &self, @@ -737,7 +737,7 @@ mod decl { Ok(PyIterReturn::Return((new_value, new_key))) } } - impl IterNextIterable for PyItertoolsGroupBy {} + impl SelfIter for PyItertoolsGroupBy {} impl IterNext for PyItertoolsGroupBy { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let mut state = zelf.state.lock(); @@ -795,9 +795,9 @@ mod decl { groupby: PyRef, } - #[pyclass(with(IterNext))] + #[pyclass(with(IterNext, Iterable))] impl PyItertoolsGrouper {} - impl IterNextIterable for PyItertoolsGrouper {} + impl SelfIter for PyItertoolsGrouper {} impl IterNext for PyItertoolsGrouper { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let old_key = { @@ -865,7 +865,7 @@ mod decl { ))) } - #[pyclass(with(IterNext), flags(BASETYPE))] + #[pyclass(with(IterNext, Iterable), flags(BASETYPE))] impl PyItertoolsIslice { #[pyslot] fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { @@ -959,7 +959,7 @@ mod decl { } } - impl IterNextIterable for PyItertoolsIslice {} + impl SelfIter for PyItertoolsIslice {} impl IterNext for PyItertoolsIslice { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { while zelf.cur.load() < zelf.next.load() { @@ -1023,7 +1023,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] + #[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))] impl PyItertoolsFilterFalse { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter)) { @@ -1033,7 +1033,7 @@ mod decl { ) } } - impl IterNextIterable for PyItertoolsFilterFalse {} + impl SelfIter for PyItertoolsFilterFalse {} impl IterNext for PyItertoolsFilterFalse { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let predicate = &zelf.predicate; @@ -1091,10 +1091,10 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyItertoolsAccumulate {} - impl IterNextIterable for PyItertoolsAccumulate {} + impl SelfIter for PyItertoolsAccumulate {} impl IterNext for PyItertoolsAccumulate { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let iterable = &zelf.iterable; @@ -1199,7 +1199,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyItertoolsTee { fn from_iter(iterator: PyIter, vm: &VirtualMachine) -> PyResult { let class = PyItertoolsTee::class(&vm.ctx); @@ -1222,7 +1222,7 @@ mod decl { } } } - impl IterNextIterable for PyItertoolsTee {} + impl SelfIter for PyItertoolsTee {} impl IterNext for PyItertoolsTee { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let value = match zelf.tee_data.get_item(vm, zelf.index.load())? { @@ -1277,7 +1277,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyItertoolsProduct { fn update_idxs(&self, mut idxs: PyRwLockWriteGuard<'_, Vec>) { if idxs.len() == 0 { @@ -1302,7 +1302,7 @@ mod decl { } } } - impl IterNextIterable for PyItertoolsProduct {} + impl SelfIter for PyItertoolsProduct {} impl IterNext for PyItertoolsProduct { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { // stop signal @@ -1370,7 +1370,7 @@ mod decl { let n = pool.len(); - PyItertoolsCombinations { + Self { pool, indices: PyRwLock::new((0..r).collect()), result: PyRwLock::new(None), @@ -1382,7 +1382,7 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyItertoolsCombinations { #[pymethod(magic)] fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyTupleRef { @@ -1413,7 +1413,7 @@ mod decl { } } - impl IterNextIterable for PyItertoolsCombinations {} + impl SelfIter for PyItertoolsCombinations {} impl IterNext for PyItertoolsCombinations { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { // stop signal @@ -1512,10 +1512,10 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyItertoolsCombinationsWithReplacement {} - impl IterNextIterable for PyItertoolsCombinationsWithReplacement {} + impl SelfIter for PyItertoolsCombinationsWithReplacement {} impl IterNext for PyItertoolsCombinationsWithReplacement { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { // stop signal @@ -1609,7 +1609,7 @@ mod decl { None => n, }; - PyItertoolsPermutations { + Self { pool, indices: PyRwLock::new((0..n).collect()), cycles: PyRwLock::new((0..r.min(n)).map(|i| n - i).collect()), @@ -1622,9 +1622,9 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyItertoolsPermutations {} - impl IterNextIterable for PyItertoolsPermutations {} + impl SelfIter for PyItertoolsPermutations {} impl IterNext for PyItertoolsPermutations { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { // stop signal @@ -1725,7 +1725,7 @@ mod decl { fillvalue: PyRwLock, } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyItertoolsZipLongest { #[pymethod(magic)] fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { @@ -1747,7 +1747,7 @@ mod decl { Ok(()) } } - impl IterNextIterable for PyItertoolsZipLongest {} + impl SelfIter for PyItertoolsZipLongest {} impl IterNext for PyItertoolsZipLongest { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { if zelf.iterators.is_empty() { @@ -1794,9 +1794,9 @@ mod decl { } } - #[pyclass(with(IterNext, Constructor))] + #[pyclass(with(IterNext, Iterable, Constructor))] impl PyItertoolsPairwise {} - impl IterNextIterable for PyItertoolsPairwise {} + impl SelfIter for PyItertoolsPairwise {} impl IterNext for PyItertoolsPairwise { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { let old = match zelf.old.read().clone() { diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index e016c21a12..9854bf59ed 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -316,7 +316,7 @@ pub(super) mod _os { function::{ArgBytesLike, Either, FsPath, FuncArgs, OptionalArg}, protocol::PyIterReturn, recursion::ReprGuard, - types::{IterNext, IterNextIterable, PyStructSequence, Representable}, + types::{IterNext, Iterable, PyStructSequence, Representable, SelfIter}, vm::VirtualMachine, AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, }; @@ -791,7 +791,7 @@ pub(super) mod _os { mode: OutputMode, } - #[pyclass(with(IterNext))] + #[pyclass(with(IterNext, Iterable))] impl ScandirIterator { #[pymethod] fn close(&self) { @@ -809,7 +809,7 @@ pub(super) mod _os { zelf.close() } } - impl IterNextIterable for ScandirIterator {} + impl SelfIter for ScandirIterator {} impl IterNext for ScandirIterator { fn next(zelf: &crate::Py, vm: &VirtualMachine) -> PyResult { let entryref: &mut Option = &mut zelf.entries.write(); diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 0b8ddb6fce..4d2c273c5a 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -305,6 +305,11 @@ fn iter_wrapper(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { vm.call_special_method(&zelf, identifier!(vm, __iter__), ()) } +// PyObject_SelfIter in CPython +fn self_iter(zelf: PyObjectRef, _vm: &VirtualMachine) -> PyResult { + Ok(zelf) +} + fn iternext_wrapper(zelf: &PyObject, vm: &VirtualMachine) -> PyResult { PyIterReturn::from_pyresult( vm.call_special_method(zelf, identifier!(vm, __next__), ()), @@ -1229,7 +1234,6 @@ pub trait AsNumber: PyPayload { #[pyclass] pub trait Iterable: PyPayload { #[pyslot] - #[pymethod(name = "__iter__")] fn slot_iter(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { let zelf = zelf .downcast() @@ -1237,7 +1241,14 @@ pub trait Iterable: PyPayload { Self::iter(zelf, vm) } + #[pymethod] + fn __iter__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { + Self::slot_iter(zelf, vm) + } + fn iter(zelf: PyRef, vm: &VirtualMachine) -> PyResult; + + fn extend_slots(_slots: &mut PyTypeSlots) {} } // `Iterator` fits better, but to avoid confusion with rust std::iter::Iterator @@ -1260,19 +1271,29 @@ pub trait IterNext: PyPayload + Iterable { } } -pub trait IterNextIterable: PyPayload {} +pub trait SelfIter: PyPayload {} impl Iterable for T where - T: IterNextIterable, + T: SelfIter, { - #[inline] - fn slot_iter(zelf: PyObjectRef, _vm: &VirtualMachine) -> PyResult { - Ok(zelf) + #[cold] + fn slot_iter(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { + let repr = zelf.repr(vm)?; + unreachable!("slot must be overriden for {}", repr.as_str()); + } + + fn __iter__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self_iter(zelf, vm) } #[cold] fn iter(_zelf: PyRef, _vm: &VirtualMachine) -> PyResult { unreachable!("slot_iter is implemented"); } + + fn extend_slots(slots: &mut PyTypeSlots) { + let prev = slots.iter.swap(Some(self_iter)); + debug_assert!(prev.is_some()); // slot_iter would be set + } } diff --git a/wasm/lib/src/js_module.rs b/wasm/lib/src/js_module.rs index e71130c5cc..7cdfd9d398 100644 --- a/wasm/lib/src/js_module.rs +++ b/wasm/lib/src/js_module.rs @@ -14,7 +14,7 @@ mod _js { convert::{IntoObject, ToPyObject}, function::{ArgCallable, OptionalArg, OptionalOption, PosArgs}, protocol::PyIterReturn, - types::{IterNext, IterNextIterable, Representable}, + types::{IterNext, Representable, SelfIter}, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine, }; use std::{cell, fmt, future}; @@ -602,7 +602,7 @@ mod _js { } } - impl IterNextIterable for AwaitPromise {} + impl SelfIter for AwaitPromise {} impl IterNext for AwaitPromise { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.send(None, vm)