Skip to content

Commit 6618def

Browse files
authored
Merge pull request RustPython#763 from adrian17/more-args
Convert more functions to new args style
2 parents 149aefe + 8f1ec3d commit 6618def

File tree

5 files changed

+42
-82
lines changed

5 files changed

+42
-82
lines changed

vm/src/obj/objenumerate.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::ops::AddAssign;
44
use num_bigint::BigInt;
55
use num_traits::Zero;
66

7-
use crate::function::{OptionalArg, PyFuncArgs};
8-
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
7+
use crate::function::OptionalArg;
8+
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
99
use crate::vm::VirtualMachine;
1010

1111
use super::objint::PyIntRef;
@@ -44,18 +44,10 @@ fn enumerate_new(
4444
.into_ref_with_type(vm, cls)
4545
}
4646

47-
fn enumerate_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
48-
arg_check!(
49-
vm,
50-
args,
51-
required = [(enumerate, Some(vm.ctx.enumerate_type()))]
52-
);
53-
54-
if let Some(PyEnumerate {
55-
ref counter,
56-
ref iterator,
57-
}) = enumerate.payload()
58-
{
47+
impl PyEnumerateRef {
48+
fn next(self, vm: &VirtualMachine) -> PyResult {
49+
let iterator = &self.iterator;
50+
let counter = &self.counter;
5951
let next_obj = objiter::call_next(vm, iterator)?;
6052
let result = vm
6153
.ctx
@@ -64,8 +56,6 @@ fn enumerate_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
6456
AddAssign::add_assign(&mut counter.borrow_mut() as &mut BigInt, 1);
6557

6658
Ok(result)
67-
} else {
68-
panic!("enumerate doesn't have correct payload");
6959
}
7060
}
7161

@@ -74,6 +64,6 @@ pub fn init(context: &PyContext) {
7464
objiter::iter_type_init(context, enumerate_type);
7565
extend_class!(context, enumerate_type, {
7666
"__new__" => context.new_rustfunc(enumerate_new),
77-
"__next__" => context.new_rustfunc(enumerate_next)
67+
"__next__" => context.new_rustfunc(PyEnumerateRef::next)
7868
});
7969
}

vm/src/obj/objfilter.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::function::PyFuncArgs;
2-
use crate::pyobject::{IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
1+
use crate::pyobject::{IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
32
use crate::vm::VirtualMachine; // Required for arg_check! to use isinstance
43

54
use super::objbool;
@@ -35,14 +34,10 @@ fn filter_new(
3534
.into_ref_with_type(vm, cls)
3635
}
3736

38-
fn filter_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
39-
arg_check!(vm, args, required = [(filter, Some(vm.ctx.filter_type()))]);
40-
41-
if let Some(PyFilter {
42-
ref predicate,
43-
ref iterator,
44-
}) = filter.payload()
45-
{
37+
impl PyFilterRef {
38+
fn next(self, vm: &VirtualMachine) -> PyResult {
39+
let predicate = &self.predicate;
40+
let iterator = &self.iterator;
4641
loop {
4742
let next_obj = objiter::call_next(vm, iterator)?;
4843
let predicate_value = if predicate.is(&vm.get_none()) {
@@ -56,8 +51,6 @@ fn filter_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
5651
return Ok(next_obj);
5752
}
5853
}
59-
} else {
60-
panic!("filter doesn't have correct payload");
6154
}
6255
}
6356

@@ -74,6 +67,6 @@ pub fn init(context: &PyContext) {
7467
extend_class!(context, filter_type, {
7568
"__new__" => context.new_rustfunc(filter_new),
7669
"__doc__" => context.new_str(filter_doc.to_string()),
77-
"__next__" => context.new_rustfunc(filter_next)
70+
"__next__" => context.new_rustfunc(PyFilterRef::next)
7871
});
7972
}

vm/src/obj/objmap.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::function::{Args, PyFuncArgs};
2-
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
1+
use crate::function::Args;
2+
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
33
use crate::vm::VirtualMachine;
44

55
use super::objiter;
@@ -35,23 +35,16 @@ fn map_new(
3535
.into_ref_with_type(vm, cls.clone())
3636
}
3737

38-
fn map_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
39-
arg_check!(vm, args, required = [(map, Some(vm.ctx.map_type()))]);
40-
41-
if let Some(PyMap {
42-
ref mapper,
43-
ref iterators,
44-
}) = map.payload()
45-
{
46-
let next_objs = iterators
38+
impl PyMapRef {
39+
fn next(self, vm: &VirtualMachine) -> PyResult {
40+
let next_objs = self
41+
.iterators
4742
.iter()
4843
.map(|iterator| objiter::call_next(vm, iterator))
4944
.collect::<Result<Vec<_>, _>>()?;
5045

5146
// the mapper itself can raise StopIteration which does stop the map iteration
52-
vm.invoke(mapper.clone(), next_objs)
53-
} else {
54-
panic!("map doesn't have correct payload");
47+
vm.invoke(self.mapper.clone(), next_objs)
5548
}
5649
}
5750

@@ -65,7 +58,7 @@ pub fn init(context: &PyContext) {
6558
objiter::iter_type_init(context, map_type);
6659
extend_class!(context, map_type, {
6760
"__new__" => context.new_rustfunc(map_new),
68-
"__next__" => context.new_rustfunc(map_next),
61+
"__next__" => context.new_rustfunc(PyMapRef::next),
6962
"__doc__" => context.new_str(map_doc.to_string())
7063
});
7164
}

vm/src/obj/objslice.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,38 +66,25 @@ fn slice_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
6666
.map(|x| x.into_object())
6767
}
6868

69-
fn get_property_value(vm: &VirtualMachine, value: &Option<BigInt>) -> PyResult {
69+
fn get_property_value(vm: &VirtualMachine, value: &Option<BigInt>) -> PyObjectRef {
7070
if let Some(value) = value {
71-
Ok(vm.ctx.new_int(value.clone()))
71+
vm.ctx.new_int(value.clone())
7272
} else {
73-
Ok(vm.get_none())
73+
vm.get_none()
7474
}
7575
}
7676

77-
fn slice_start(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
78-
arg_check!(vm, args, required = [(slice, Some(vm.ctx.slice_type()))]);
79-
if let Some(PySlice { start, .. }) = &slice.payload() {
80-
get_property_value(vm, start)
81-
} else {
82-
panic!("Slice has incorrect payload.");
77+
impl PySliceRef {
78+
fn start(self, vm: &VirtualMachine) -> PyObjectRef {
79+
get_property_value(vm, &self.start)
8380
}
84-
}
8581

86-
fn slice_stop(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
87-
arg_check!(vm, args, required = [(slice, Some(vm.ctx.slice_type()))]);
88-
if let Some(PySlice { stop, .. }) = &slice.payload() {
89-
get_property_value(vm, stop)
90-
} else {
91-
panic!("Slice has incorrect payload.");
82+
fn stop(self, vm: &VirtualMachine) -> PyObjectRef {
83+
get_property_value(vm, &self.stop)
9284
}
93-
}
9485

95-
fn slice_step(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
96-
arg_check!(vm, args, required = [(slice, Some(vm.ctx.slice_type()))]);
97-
if let Some(PySlice { step, .. }) = &slice.payload() {
98-
get_property_value(vm, step)
99-
} else {
100-
panic!("Slice has incorrect payload.");
86+
fn step(self, vm: &VirtualMachine) -> PyObjectRef {
87+
get_property_value(vm, &self.step)
10188
}
10289
}
10390

@@ -106,8 +93,8 @@ pub fn init(context: &PyContext) {
10693

10794
extend_class!(context, slice_type, {
10895
"__new__" => context.new_rustfunc(slice_new),
109-
"start" => context.new_property(slice_start),
110-
"stop" => context.new_property(slice_stop),
111-
"step" => context.new_property(slice_step)
96+
"start" => context.new_property(PySliceRef::start),
97+
"stop" => context.new_property(PySliceRef::stop),
98+
"step" => context.new_property(PySliceRef::step)
11299
});
113100
}

vm/src/obj/objzip.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::function::{Args, PyFuncArgs};
2-
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
1+
use crate::function::Args;
2+
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
33
use crate::vm::VirtualMachine;
44

55
use super::objiter;
@@ -26,22 +26,19 @@ fn zip_new(cls: PyClassRef, iterables: Args, vm: &VirtualMachine) -> PyResult<Py
2626
PyZip { iterators }.into_ref_with_type(vm, cls)
2727
}
2828

29-
fn zip_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
30-
arg_check!(vm, args, required = [(zip, Some(vm.ctx.zip_type()))]);
31-
32-
if let Some(PyZip { ref iterators }) = zip.payload() {
33-
if iterators.is_empty() {
29+
impl PyZipRef {
30+
fn next(self, vm: &VirtualMachine) -> PyResult {
31+
if self.iterators.is_empty() {
3432
Err(objiter::new_stop_iteration(vm))
3533
} else {
36-
let next_objs = iterators
34+
let next_objs = self
35+
.iterators
3736
.iter()
3837
.map(|iterator| objiter::call_next(vm, iterator))
3938
.collect::<Result<Vec<_>, _>>()?;
4039

4140
Ok(vm.ctx.new_tuple(next_objs))
4241
}
43-
} else {
44-
panic!("zip doesn't have correct payload");
4542
}
4643
}
4744

@@ -50,6 +47,6 @@ pub fn init(context: &PyContext) {
5047
objiter::iter_type_init(context, zip_type);
5148
extend_class!(context, zip_type, {
5249
"__new__" => context.new_rustfunc(zip_new),
53-
"__next__" => context.new_rustfunc(zip_next)
50+
"__next__" => context.new_rustfunc(PyZipRef::next)
5451
});
5552
}

0 commit comments

Comments
 (0)