Skip to content

Commit 8f1ec3d

Browse files
committed
Convert slice::{start, stop, stop} to new args style
1 parent d5e2b3a commit 8f1ec3d

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

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
}

0 commit comments

Comments
 (0)