Skip to content

Commit 56bb6d2

Browse files
authored
Merge pull request RustPython#634 from RustPython/joey/map-zip-filter-enumerate-to-any
Convert map, zip, filter, and enumerate to Any payload
2 parents 3730ca2 + a2951f8 commit 56bb6d2

File tree

5 files changed

+93
-44
lines changed

5 files changed

+93
-44
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/obj/objfilter.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
use super::objbool;
2-
use super::objiter;
31
use crate::pyobject::{
4-
IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyResult, TypeProtocol,
2+
IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef,
3+
PyResult, TypeProtocol,
54
};
65
use crate::vm::VirtualMachine; // Required for arg_check! to use isinstance
76

7+
use super::objbool;
8+
use super::objiter;
9+
10+
#[derive(Debug)]
11+
pub struct PyFilter {
12+
predicate: PyObjectRef,
13+
iterator: PyObjectRef,
14+
}
15+
16+
impl PyObjectPayload2 for PyFilter {
17+
fn required_type(ctx: &PyContext) -> PyObjectRef {
18+
ctx.filter_type()
19+
}
20+
}
21+
822
fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
923
arg_check!(
1024
vm,
@@ -13,9 +27,11 @@ fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1327
);
1428
let iterator = objiter::get_iter(vm, iterable)?;
1529
Ok(PyObject::new(
16-
PyObjectPayload::FilterIterator {
17-
predicate: function.clone(),
18-
iterator,
30+
PyObjectPayload::AnyRustValue {
31+
value: Box::new(PyFilter {
32+
predicate: function.clone(),
33+
iterator,
34+
}),
1935
},
2036
cls.clone(),
2137
))
@@ -24,10 +40,10 @@ fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2440
fn filter_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2541
arg_check!(vm, args, required = [(filter, Some(vm.ctx.filter_type()))]);
2642

27-
if let PyObjectPayload::FilterIterator {
43+
if let Some(PyFilter {
2844
ref predicate,
2945
ref iterator,
30-
} = filter.payload
46+
}) = filter.payload()
3147
{
3248
loop {
3349
let next_obj = objiter::call_next(vm, iterator)?;

vm/src/obj/objmap.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
use crate::pyobject::{
2+
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
3+
TypeProtocol,
4+
};
5+
use crate::vm::VirtualMachine;
6+
17
use super::objiter;
2-
use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyResult, TypeProtocol};
3-
use crate::vm::VirtualMachine; // Required for arg_check! to use isinstance
8+
9+
#[derive(Debug)]
10+
pub struct PyMap {
11+
mapper: PyObjectRef,
12+
iterators: Vec<PyObjectRef>,
13+
}
14+
15+
impl PyObjectPayload2 for PyMap {
16+
fn required_type(ctx: &PyContext) -> PyObjectRef {
17+
ctx.map_type()
18+
}
19+
}
420

521
fn map_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
622
no_kwargs!(vm, args);
@@ -15,9 +31,11 @@ fn map_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1531
.map(|iterable| objiter::get_iter(vm, iterable))
1632
.collect::<Result<Vec<_>, _>>()?;
1733
Ok(PyObject::new(
18-
PyObjectPayload::MapIterator {
19-
mapper: function.clone(),
20-
iterators,
34+
PyObjectPayload::AnyRustValue {
35+
value: Box::new(PyMap {
36+
mapper: function.clone(),
37+
iterators,
38+
}),
2139
},
2240
cls.clone(),
2341
))
@@ -27,10 +45,10 @@ fn map_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2745
fn map_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2846
arg_check!(vm, args, required = [(map, Some(vm.ctx.map_type()))]);
2947

30-
if let PyObjectPayload::MapIterator {
48+
if let Some(PyMap {
3149
ref mapper,
3250
ref iterators,
33-
} = map.payload
51+
}) = map.payload()
3452
{
3553
let next_objs = iterators
3654
.iter()

vm/src/obj/objzip.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
use crate::pyobject::{
2+
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
3+
TypeProtocol,
4+
};
5+
use crate::vm::VirtualMachine;
6+
17
use super::objiter;
2-
use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyResult, TypeProtocol};
3-
use crate::vm::VirtualMachine; // Required for arg_check! to use isinstance
8+
9+
#[derive(Debug)]
10+
pub struct PyZip {
11+
iterators: Vec<PyObjectRef>,
12+
}
13+
14+
impl PyObjectPayload2 for PyZip {
15+
fn required_type(ctx: &PyContext) -> PyObjectRef {
16+
ctx.zip_type()
17+
}
18+
}
419

520
fn zip_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
621
no_kwargs!(vm, args);
@@ -11,15 +26,17 @@ fn zip_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1126
.map(|iterable| objiter::get_iter(vm, iterable))
1227
.collect::<Result<Vec<_>, _>>()?;
1328
Ok(PyObject::new(
14-
PyObjectPayload::ZipIterator { iterators },
29+
PyObjectPayload::AnyRustValue {
30+
value: Box::new(PyZip { iterators }),
31+
},
1532
cls.clone(),
1633
))
1734
}
1835

1936
fn zip_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2037
arg_check!(vm, args, required = [(zip, Some(vm.ctx.zip_type()))]);
2138

22-
if let PyObjectPayload::ZipIterator { ref iterators } = zip.payload {
39+
if let Some(PyZip { ref iterators }) = zip.payload() {
2340
if iterators.is_empty() {
2441
Err(objiter::new_stop_iteration(vm))
2542
} else {

vm/src/pyobject.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,21 +1499,6 @@ pub enum PyObjectPayload {
14991499
position: Cell<usize>,
15001500
iterated_obj: PyObjectRef,
15011501
},
1502-
EnumerateIterator {
1503-
counter: RefCell<BigInt>,
1504-
iterator: PyObjectRef,
1505-
},
1506-
FilterIterator {
1507-
predicate: PyObjectRef,
1508-
iterator: PyObjectRef,
1509-
},
1510-
MapIterator {
1511-
mapper: PyObjectRef,
1512-
iterators: Vec<PyObjectRef>,
1513-
},
1514-
ZipIterator {
1515-
iterators: Vec<PyObjectRef>,
1516-
},
15171502
Slice {
15181503
start: Option<BigInt>,
15191504
stop: Option<BigInt>,
@@ -1562,10 +1547,6 @@ impl fmt::Debug for PyObjectPayload {
15621547
PyObjectPayload::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
15631548
PyObjectPayload::WeakRef { .. } => write!(f, "weakref"),
15641549
PyObjectPayload::Iterator { .. } => write!(f, "iterator"),
1565-
PyObjectPayload::EnumerateIterator { .. } => write!(f, "enumerate"),
1566-
PyObjectPayload::FilterIterator { .. } => write!(f, "filter"),
1567-
PyObjectPayload::MapIterator { .. } => write!(f, "map"),
1568-
PyObjectPayload::ZipIterator { .. } => write!(f, "zip"),
15691550
PyObjectPayload::Slice { .. } => write!(f, "slice"),
15701551
PyObjectPayload::Function { .. } => write!(f, "function"),
15711552
PyObjectPayload::Generator { .. } => write!(f, "generator"),

0 commit comments

Comments
 (0)