mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-17 01:51:39 +09:00
Add range.__eq__
This commit is contained in:
@@ -13,7 +13,7 @@ use crate::vm::VirtualMachine;
|
||||
use super::objint::{PyInt, PyIntRef};
|
||||
use super::objiter;
|
||||
use super::objslice::{PySlice, PySliceRef};
|
||||
use super::objtype::PyClassRef;
|
||||
use super::objtype::{self, PyClassRef};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PyRange {
|
||||
@@ -110,6 +110,7 @@ pub fn init(context: &PyContext) {
|
||||
"__bool__" => context.new_rustfunc(PyRange::bool),
|
||||
"__contains__" => context.new_rustfunc(PyRange::contains),
|
||||
"__doc__" => context.new_str(range_doc.to_string()),
|
||||
"__eq__" => context.new_rustfunc(PyRange::eq),
|
||||
"__getitem__" => context.new_rustfunc(PyRange::getitem),
|
||||
"__iter__" => context.new_rustfunc(PyRange::iter),
|
||||
"__len__" => context.new_rustfunc(PyRange::len),
|
||||
@@ -246,6 +247,17 @@ impl PyRange {
|
||||
}
|
||||
}
|
||||
|
||||
fn eq(&self, rhs: PyObjectRef, vm: &VirtualMachine) -> bool {
|
||||
if objtype::isinstance(&rhs, &vm.ctx.range_type()) {
|
||||
let rhs = get_value(&rhs);
|
||||
self.start.as_bigint() == rhs.start.as_bigint()
|
||||
&& self.stop.as_bigint() == rhs.stop.as_bigint()
|
||||
&& self.step.as_bigint() == rhs.step.as_bigint()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn index(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyInt> {
|
||||
if let Ok(int) = needle.downcast::<PyInt>() {
|
||||
match self.index_of(int.as_bigint()) {
|
||||
|
||||
Reference in New Issue
Block a user