diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index 5ce46c1b41..36e6d3a7f1 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -556,24 +556,30 @@ impl PyList { } } + #[inline] + fn cmp(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyResult + where + F: Fn(&Vec, &Vec) -> PyResult, + { + let r = if let Some(other) = other.payload_if_subclass::(vm) { + Implemented(op(&*self.borrow_elements(), &*other.borrow_elements())?) + } else { + NotImplemented + }; + Ok(r) + } + #[pymethod(name = "__eq__")] fn eq( zelf: PyRef, other: PyObjectRef, vm: &VirtualMachine, ) -> PyResult { - let value = if zelf.as_object().is(&other) { - Implemented(true) - } else if let Some(other) = other.payload_if_subclass::(vm) { - Implemented(sequence::eq( - vm, - &zelf.borrow_sequence(), - &other.borrow_sequence(), - )?) + if zelf.as_object().is(&other) { + Ok(Implemented(true)) } else { - NotImplemented - }; - Ok(value) + zelf.cmp(other, |a, b| sequence::eq(vm, a, b), vm) + } } #[pymethod(name = "__ne__")] @@ -586,43 +592,23 @@ impl PyList { } #[pymethod(name = "__lt__")] - fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { - if let Some(other) = other.payload_if_subclass::(vm) { - let res = sequence::lt(vm, &self.borrow_sequence(), &other.borrow_sequence())?; - Ok(vm.new_bool(res)) - } else { - Ok(vm.ctx.not_implemented()) - } + fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.cmp(other, |a, b| sequence::lt(vm, a, b), vm) } #[pymethod(name = "__gt__")] - fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { - if let Some(other) = other.payload_if_subclass::(vm) { - let res = sequence::gt(vm, &self.borrow_sequence(), &other.borrow_sequence())?; - Ok(vm.new_bool(res)) - } else { - Ok(vm.ctx.not_implemented()) - } + fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.cmp(other, |a, b| sequence::gt(vm, a, b), vm) } #[pymethod(name = "__ge__")] - fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { - if let Some(other) = other.payload_if_subclass::(vm) { - let res = sequence::ge(vm, &self.borrow_sequence(), &other.borrow_sequence())?; - Ok(vm.new_bool(res)) - } else { - Ok(vm.ctx.not_implemented()) - } + fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.cmp(other, |a, b| sequence::ge(vm, a, b), vm) } #[pymethod(name = "__le__")] - fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { - if let Some(other) = other.payload_if_subclass::(vm) { - let res = sequence::le(vm, &self.borrow_sequence(), &other.borrow_sequence())?; - Ok(vm.new_bool(res)) - } else { - Ok(vm.ctx.not_implemented()) - } + fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + self.cmp(other, |a, b| sequence::le(vm, a, b), vm) } #[pymethod(name = "__delitem__")]