Implements Comparable for PyDictKeys, PyDictItems

This commit is contained in:
zetwhite
2021-10-23 14:38:28 +09:00
parent 7f0ba0c176
commit a3fed90fc2

View File

@@ -746,34 +746,6 @@ macro_rules! dict_view {
}
}
impl Comparable for $name {
fn cmp(
zelf: &PyObjectView<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
match_class!(match other {
ref dictview @ Self => {
PyDict::inner_cmp(
&zelf.dict,
&dictview.dict,
op,
!zelf.class().is(&vm.ctx.types.dict_keys_type),
vm,
)
}
ref _set @ PySet => {
// TODO: Implement comparison for set
Ok(NotImplemented)
}
_ => {
Ok(NotImplemented)
}
})
}
}
impl PyValue for $name {
fn class(vm: &VirtualMachine) -> &PyTypeRef {
&vm.ctx.types.$class
@@ -997,6 +969,35 @@ impl PyDictKeys {
}
impl Unconstructible for PyDictKeys {}
impl Comparable for PyDictKeys {
fn cmp(
zelf: &PyObjectView<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
match_class!(match other {
ref dictview @ Self => {
PyDict::inner_cmp(
&zelf.dict,
&dictview.dict,
op,
!zelf.class().is(&vm.ctx.types.dict_keys_type),
vm,
)
}
ref _set @ PySet => {
let inner = Self::to_set(zelf.to_owned(), vm)?;
let zelf_set = PySet { inner }.into_object(vm);
PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm)
}
_ => {
Ok(NotImplemented)
}
})
}
}
impl ViewSetOps for PyDictItems {}
#[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))]
impl PyDictItems {
@@ -1022,7 +1023,36 @@ impl PyDictItems {
}
impl Unconstructible for PyDictItems {}
#[pyimpl(with(DictView, Constructor, Comparable, Iterable))]
impl Comparable for PyDictItems {
fn cmp(
zelf: &PyObjectView<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
match_class!(match other {
ref dictview @ Self => {
PyDict::inner_cmp(
&zelf.dict,
&dictview.dict,
op,
!zelf.class().is(&vm.ctx.types.dict_keys_type),
vm,
)
}
ref _set @ PySet => {
let inner = Self::to_set(zelf.to_owned(), vm)?;
let zelf_set = PySet { inner }.into_object(vm);
PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm)
}
_ => {
Ok(NotImplemented)
}
})
}
}
#[pyimpl(with(DictView, Constructor, Iterable))]
impl PyDictValues {}
impl Unconstructible for PyDictValues {}