Skip to content

Commit eb14ada

Browse files
Merge pull request #645 from RustPython/joey/convert-iterator
Convert iterator to Any payload
2 parents 79b4f62 + 157d18d commit eb14ada

File tree

8 files changed

+79
-63
lines changed

8 files changed

+79
-63
lines changed

vm/src/obj/objbytes.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::ops::Deref;
55
use super::objint;
66
use super::objtype;
77
use crate::pyobject::{
8-
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
9-
TypeProtocol,
8+
PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2,
9+
PyObjectRef, PyResult, TypeProtocol,
1010
};
1111
use crate::vm::VirtualMachine;
1212
use num_traits::ToPrimitive;
@@ -209,9 +209,11 @@ fn bytes_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
209209
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytes_type()))]);
210210

211211
let iter_obj = PyObject::new(
212-
PyObjectPayload::Iterator {
213-
position: Cell::new(0),
214-
iterated_obj: obj.clone(),
212+
PyObjectPayload::AnyRustValue {
213+
value: Box::new(PyIteratorValue {
214+
position: Cell::new(0),
215+
iterated_obj: obj.clone(),
216+
}),
215217
},
216218
vm.ctx.iter_type(),
217219
);

vm/src/obj/objdict.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use super::objiter;
66
use super::objstr;
77
use super::objtype;
88
use crate::pyobject::{
9-
PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef,
10-
PyResult, TypeProtocol,
9+
PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload,
10+
PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol,
1111
};
1212
use crate::vm::{ReprGuard, VirtualMachine};
1313

@@ -249,9 +249,11 @@ fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
249249
let key_list = vm.ctx.new_list(keys);
250250

251251
let iter_obj = PyObject::new(
252-
PyObjectPayload::Iterator {
253-
position: Cell::new(0),
254-
iterated_obj: key_list,
252+
PyObjectPayload::AnyRustValue {
253+
value: Box::new(PyIteratorValue {
254+
position: Cell::new(0),
255+
iterated_obj: key_list,
256+
}),
255257
},
256258
vm.ctx.iter_type(),
257259
);
@@ -269,9 +271,11 @@ fn dict_values(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
269271
let values_list = vm.ctx.new_list(values);
270272

271273
let iter_obj = PyObject::new(
272-
PyObjectPayload::Iterator {
273-
position: Cell::new(0),
274-
iterated_obj: values_list,
274+
PyObjectPayload::AnyRustValue {
275+
value: Box::new(PyIteratorValue {
276+
position: Cell::new(0),
277+
iterated_obj: values_list,
278+
}),
275279
},
276280
vm.ctx.iter_type(),
277281
);
@@ -289,9 +293,11 @@ fn dict_items(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
289293
let items_list = vm.ctx.new_list(items);
290294

291295
let iter_obj = PyObject::new(
292-
PyObjectPayload::Iterator {
293-
position: Cell::new(0),
294-
iterated_obj: items_list,
296+
PyObjectPayload::AnyRustValue {
297+
value: Box::new(PyIteratorValue {
298+
position: Cell::new(0),
299+
iterated_obj: items_list,
300+
}),
295301
},
296302
vm.ctx.iter_type(),
297303
);

vm/src/obj/objiter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
use crate::pyobject::{
6-
PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
6+
PyContext, PyFuncArgs, PyIteratorValue, PyObjectRef, PyResult, TypeProtocol,
77
};
88
use crate::vm::VirtualMachine;
99

@@ -128,10 +128,10 @@ fn iter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
128128
fn iter_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
129129
arg_check!(vm, args, required = [(iter, Some(vm.ctx.iter_type()))]);
130130

131-
if let PyObjectPayload::Iterator {
131+
if let Some(PyIteratorValue {
132132
ref position,
133133
iterated_obj: ref iterated_obj_ref,
134-
} = iter.payload
134+
}) = iter.payload()
135135
{
136136
if let Some(range) = iterated_obj_ref.payload::<PyRange>() {
137137
if let Some(int) = range.get(position.get()) {

vm/src/obj/objlist.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use super::objstr;
1010
use super::objtype;
1111
use crate::function::PyRef;
1212
use crate::pyobject::{
13-
IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2,
14-
PyObjectRef, PyResult, TypeProtocol,
13+
IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload,
14+
PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol,
1515
};
1616
use crate::vm::{ReprGuard, VirtualMachine};
1717
use num_traits::ToPrimitive;
@@ -112,9 +112,11 @@ impl PyListRef {
112112

113113
fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef {
114114
PyObject::new(
115-
PyObjectPayload::Iterator {
116-
position: Cell::new(0),
117-
iterated_obj: self.into_object(),
115+
PyObjectPayload::AnyRustValue {
116+
value: Box::new(PyIteratorValue {
117+
position: Cell::new(0),
118+
iterated_obj: self.into_object(),
119+
}),
118120
},
119121
vm.ctx.iter_type(),
120122
)

vm/src/obj/objrange.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use num_integer::Integer;
66
use num_traits::{One, Signed, ToPrimitive, Zero};
77

88
use crate::pyobject::{
9-
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
10-
TypeProtocol,
9+
PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2,
10+
PyObjectRef, PyResult, TypeProtocol,
1111
};
1212
use crate::vm::VirtualMachine;
1313

@@ -240,9 +240,11 @@ fn range_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
240240
arg_check!(vm, args, required = [(range, Some(vm.ctx.range_type()))]);
241241

242242
Ok(PyObject::new(
243-
PyObjectPayload::Iterator {
244-
position: Cell::new(0),
245-
iterated_obj: range.clone(),
243+
PyObjectPayload::AnyRustValue {
244+
value: Box::new(PyIteratorValue {
245+
position: Cell::new(0),
246+
iterated_obj: range.clone(),
247+
}),
246248
},
247249
vm.ctx.iter_type(),
248250
))
@@ -254,14 +256,16 @@ fn range_reversed(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
254256
let range = get_value(zelf).reversed();
255257

256258
Ok(PyObject::new(
257-
PyObjectPayload::Iterator {
258-
position: Cell::new(0),
259-
iterated_obj: PyObject::new(
260-
PyObjectPayload::AnyRustValue {
261-
value: Box::new(range),
262-
},
263-
vm.ctx.range_type(),
264-
),
259+
PyObjectPayload::AnyRustValue {
260+
value: Box::new(PyIteratorValue {
261+
position: Cell::new(0),
262+
iterated_obj: PyObject::new(
263+
PyObjectPayload::AnyRustValue {
264+
value: Box::new(range),
265+
},
266+
vm.ctx.range_type(),
267+
),
268+
}),
265269
},
266270
vm.ctx.iter_type(),
267271
))

vm/src/obj/objset.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use super::objiter;
1313
use super::objstr;
1414
use super::objtype;
1515
use crate::pyobject::{
16-
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
17-
TypeProtocol,
16+
PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2,
17+
PyObjectRef, PyResult, TypeProtocol,
1818
};
1919
use crate::vm::{ReprGuard, VirtualMachine};
2020

@@ -566,9 +566,11 @@ fn set_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
566566
let items = get_elements(zelf).values().cloned().collect();
567567
let set_list = vm.ctx.new_list(items);
568568
let iter_obj = PyObject::new(
569-
PyObjectPayload::Iterator {
570-
position: Cell::new(0),
571-
iterated_obj: set_list,
569+
PyObjectPayload::AnyRustValue {
570+
value: Box::new(PyIteratorValue {
571+
position: Cell::new(0),
572+
iterated_obj: set_list,
573+
}),
572574
},
573575
vm.ctx.iter_type(),
574576
);

vm/src/obj/objtuple.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::hash::{Hash, Hasher};
33

44
use crate::function::PyRef;
55
use crate::pyobject::{
6-
IdProtocol, OptionalArg, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef,
7-
PyResult,
6+
IdProtocol, OptionalArg, PyContext, PyIteratorValue, PyObject, PyObjectPayload,
7+
PyObjectPayload2, PyObjectRef, PyResult,
88
};
99
use crate::vm::{ReprGuard, VirtualMachine};
1010

@@ -127,9 +127,11 @@ impl PyTupleRef {
127127

128128
fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef {
129129
PyObject::new(
130-
PyObjectPayload::Iterator {
131-
position: Cell::new(0),
132-
iterated_obj: self.into_object(),
130+
PyObjectPayload::AnyRustValue {
131+
value: Box::new(PyIteratorValue {
132+
position: Cell::new(0),
133+
iterated_obj: self.into_object(),
134+
}),
133135
},
134136
vm.ctx.iter_type(),
135137
)

vm/src/pyobject.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,6 @@ impl fmt::Display for PyObject {
100100
}
101101
}
102102

103-
/*
104-
// Idea: implement the iterator trait upon PyObjectRef
105-
impl Iterator for (VirtualMachine, PyObjectRef) {
106-
type Item = char;
107-
108-
fn next(&mut self) -> Option<Self::Item> {
109-
// call method ("_next__")
110-
}
111-
}
112-
*/
113-
114103
#[derive(Debug)]
115104
pub struct PyContext {
116105
pub bytes_type: PyObjectRef,
@@ -1511,10 +1500,6 @@ into_py_native_func_tuple!((a, A), (b, B), (c, C), (d, D), (e, E));
15111500
/// of rust data for a particular python object. Determine the python type
15121501
/// by using for example the `.typ()` method on a python object.
15131502
pub enum PyObjectPayload {
1514-
Iterator {
1515-
position: Cell<usize>,
1516-
iterated_obj: PyObjectRef,
1517-
},
15181503
Slice {
15191504
start: Option<BigInt>,
15201505
stop: Option<BigInt>,
@@ -1539,12 +1524,25 @@ impl Default for PyObjectPayload {
15391524
}
15401525
}
15411526

1527+
// TODO: This is a workaround and shouldn't exist.
1528+
// Each iterable type should have its own distinct iterator type.
1529+
#[derive(Debug)]
1530+
pub struct PyIteratorValue {
1531+
pub position: Cell<usize>,
1532+
pub iterated_obj: PyObjectRef,
1533+
}
1534+
1535+
impl PyObjectPayload2 for PyIteratorValue {
1536+
fn required_type(ctx: &PyContext) -> PyObjectRef {
1537+
ctx.iter_type()
1538+
}
1539+
}
1540+
15421541
impl fmt::Debug for PyObjectPayload {
15431542
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15441543
match self {
15451544
PyObjectPayload::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
15461545
PyObjectPayload::WeakRef { .. } => write!(f, "weakref"),
1547-
PyObjectPayload::Iterator { .. } => write!(f, "iterator"),
15481546
PyObjectPayload::Slice { .. } => write!(f, "slice"),
15491547
PyObjectPayload::AnyRustValue { value } => value.fmt(f),
15501548
}

0 commit comments

Comments
 (0)