GeezerVison is showing github.com — Reuse the shared optional argument binder for islice · RustPython/RustPython@c756f0d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/stdlib/itertools.rs +8-21Lines changed: 8 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -801,32 +801,19 @@ mod decl { | 801 | 801 | fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { | 802 | 802 | let args = | 803 | 803 | crate::types::drop_kwargs_if_init_overridden(&cls, Self::class(&vm.ctx), args); | 804 | | - let (iter, a1, a2, a3) = match args.args.len() { | 805 | | - 0 | 1 => { | 806 | | - return Err(vm.new_arity_type_error(Self::NAME, 2..=4, args.args.len())); | 807 | | - } | 808 | | - 2 => { | 809 | | - let (iter, a1): (PyObjectRef, PyObjectRef) = args.bind_for(vm, Self::NAME)?; | 810 | | - (iter, a1, None, None) | 811 | | - } | 812 | | - 3 => { | 813 | | - let (iter, a1, a2): (PyObjectRef, PyObjectRef, PyObjectRef) = | 814 | | - args.bind_for(vm, Self::NAME)?; | 815 | | - (iter, a1, Some(a2), None) | 816 | | - } | 817 | | - _ => { | 818 | | - let (iter, a1, a2, a3): (PyObjectRef, PyObjectRef, PyObjectRef, PyObjectRef) = | 819 | | - args.bind_for(vm, Self::NAME)?; | 820 | | - (iter, a1, Some(a2), Some(a3)) | 821 | | - } | 822 | | - }; | | 804 | + let (iter, a1, a2, a3): ( | | 805 | + PyObjectRef, | | 806 | + PyObjectRef, | | 807 | + OptionalArg<PyObjectRef>, | | 808 | + OptionalArg<PyObjectRef>, | | 809 | + ) = args.bind_for(vm, Self::NAME)?; | 823 | 810 |
| 824 | 811 | // CPython clears index conversion errors and reports the appropriate | 825 | 812 | // islice ValueError after converting arguments in this order. | 826 | 813 | let to_ssize = |obj: &PyObject| isize::try_from_borrowed_object(vm, obj).unwrap_or(-1); | 827 | 814 | let mut start = 0; | 828 | 815 | let mut stop = -1; | 829 | | - if let Some(a2) = &a2 { | | 816 | + if let OptionalArg::Present(a2) = &a2 { | 830 | 817 | if !vm.is_none(&a1) { | 831 | 818 | start = to_ssize(&a1); | 832 | 819 | } | @@ -853,7 +840,7 @@ mod decl { | 853 | 840 | } | 854 | 841 |
| 855 | 842 | let mut step = 1; | 856 | | - if let Some(a3) = &a3 | | 843 | + if let OptionalArg::Present(a3) = &a3 | 857 | 844 | && !vm.is_none(a3) | 858 | 845 | { | 859 | 846 | step = to_ssize(a3); | |
|