Skip to content

Commit a2951f8

Browse files
enumerate
1 parent 9544a47 commit a2951f8

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

vm/src/obj/objenumerate.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@ use std::ops::AddAssign;
33

44
use super::objint;
55
use super::objiter;
6-
use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyResult, TypeProtocol};
6+
use crate::pyobject::{
7+
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
8+
TypeProtocol,
9+
};
710
use crate::vm::VirtualMachine;
811
use num_bigint::BigInt;
912
use num_traits::Zero;
1013

14+
#[derive(Debug)]
15+
pub struct PyEnumerate {
16+
counter: RefCell<BigInt>,
17+
iterator: PyObjectRef,
18+
}
19+
20+
impl PyObjectPayload2 for PyEnumerate {
21+
fn required_type(ctx: &PyContext) -> PyObjectRef {
22+
ctx.enumerate_type()
23+
}
24+
}
25+
1126
fn enumerate_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1227
arg_check!(
1328
vm,
@@ -22,9 +37,11 @@ fn enumerate_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2237
};
2338
let iterator = objiter::get_iter(vm, iterable)?;
2439
Ok(PyObject::new(
25-
PyObjectPayload::EnumerateIterator {
26-
counter: RefCell::new(counter),
27-
iterator,
40+
PyObjectPayload::AnyRustValue {
41+
value: Box::new(PyEnumerate {
42+
counter: RefCell::new(counter),
43+
iterator,
44+
}),
2845
},
2946
cls.clone(),
3047
))
@@ -37,10 +54,10 @@ fn enumerate_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3754
required = [(enumerate, Some(vm.ctx.enumerate_type()))]
3855
);
3956

40-
if let PyObjectPayload::EnumerateIterator {
57+
if let Some(PyEnumerate {
4158
ref counter,
4259
ref iterator,
43-
} = enumerate.payload
60+
}) = enumerate.payload()
4461
{
4562
let next_obj = objiter::call_next(vm, iterator)?;
4663
let result = vm

vm/src/pyobject.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,10 +1504,6 @@ pub enum PyObjectPayload {
15041504
position: Cell<usize>,
15051505
iterated_obj: PyObjectRef,
15061506
},
1507-
EnumerateIterator {
1508-
counter: RefCell<BigInt>,
1509-
iterator: PyObjectRef,
1510-
},
15111507
Slice {
15121508
start: Option<BigInt>,
15131509
stop: Option<BigInt>,
@@ -1560,7 +1556,6 @@ impl fmt::Debug for PyObjectPayload {
15601556
PyObjectPayload::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
15611557
PyObjectPayload::WeakRef { .. } => write!(f, "weakref"),
15621558
PyObjectPayload::Iterator { .. } => write!(f, "iterator"),
1563-
PyObjectPayload::EnumerateIterator { .. } => write!(f, "enumerate"),
15641559
PyObjectPayload::Slice { .. } => write!(f, "slice"),
15651560
PyObjectPayload::Function { .. } => write!(f, "function"),
15661561
PyObjectPayload::Generator { .. } => write!(f, "generator"),

0 commit comments

Comments
 (0)