Skip to content

Commit a3fed90

Browse files
committed
Implements Comparable for PyDictKeys, PyDictItems
1 parent 7f0ba0c commit a3fed90

1 file changed

Lines changed: 59 additions & 29 deletions

File tree

vm/src/builtins/dict.rs

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -746,34 +746,6 @@ macro_rules! dict_view {
746746
}
747747
}
748748

749-
impl Comparable for $name {
750-
fn cmp(
751-
zelf: &PyObjectView<Self>,
752-
other: &PyObject,
753-
op: PyComparisonOp,
754-
vm: &VirtualMachine,
755-
) -> PyResult<PyComparisonValue> {
756-
match_class!(match other {
757-
ref dictview @ Self => {
758-
PyDict::inner_cmp(
759-
&zelf.dict,
760-
&dictview.dict,
761-
op,
762-
!zelf.class().is(&vm.ctx.types.dict_keys_type),
763-
vm,
764-
)
765-
}
766-
ref _set @ PySet => {
767-
// TODO: Implement comparison for set
768-
Ok(NotImplemented)
769-
}
770-
_ => {
771-
Ok(NotImplemented)
772-
}
773-
})
774-
}
775-
}
776-
777749
impl PyValue for $name {
778750
fn class(vm: &VirtualMachine) -> &PyTypeRef {
779751
&vm.ctx.types.$class
@@ -997,6 +969,35 @@ impl PyDictKeys {
997969
}
998970
impl Unconstructible for PyDictKeys {}
999971

972+
impl Comparable for PyDictKeys {
973+
fn cmp(
974+
zelf: &PyObjectView<Self>,
975+
other: &PyObject,
976+
op: PyComparisonOp,
977+
vm: &VirtualMachine,
978+
) -> PyResult<PyComparisonValue> {
979+
match_class!(match other {
980+
ref dictview @ Self => {
981+
PyDict::inner_cmp(
982+
&zelf.dict,
983+
&dictview.dict,
984+
op,
985+
!zelf.class().is(&vm.ctx.types.dict_keys_type),
986+
vm,
987+
)
988+
}
989+
ref _set @ PySet => {
990+
let inner = Self::to_set(zelf.to_owned(), vm)?;
991+
let zelf_set = PySet { inner }.into_object(vm);
992+
PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm)
993+
}
994+
_ => {
995+
Ok(NotImplemented)
996+
}
997+
})
998+
}
999+
}
1000+
10001001
impl ViewSetOps for PyDictItems {}
10011002
#[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))]
10021003
impl PyDictItems {
@@ -1022,7 +1023,36 @@ impl PyDictItems {
10221023
}
10231024
impl Unconstructible for PyDictItems {}
10241025

1025-
#[pyimpl(with(DictView, Constructor, Comparable, Iterable))]
1026+
impl Comparable for PyDictItems {
1027+
fn cmp(
1028+
zelf: &PyObjectView<Self>,
1029+
other: &PyObject,
1030+
op: PyComparisonOp,
1031+
vm: &VirtualMachine,
1032+
) -> PyResult<PyComparisonValue> {
1033+
match_class!(match other {
1034+
ref dictview @ Self => {
1035+
PyDict::inner_cmp(
1036+
&zelf.dict,
1037+
&dictview.dict,
1038+
op,
1039+
!zelf.class().is(&vm.ctx.types.dict_keys_type),
1040+
vm,
1041+
)
1042+
}
1043+
ref _set @ PySet => {
1044+
let inner = Self::to_set(zelf.to_owned(), vm)?;
1045+
let zelf_set = PySet { inner }.into_object(vm);
1046+
PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm)
1047+
}
1048+
_ => {
1049+
Ok(NotImplemented)
1050+
}
1051+
})
1052+
}
1053+
}
1054+
1055+
#[pyimpl(with(DictView, Constructor, Iterable))]
10261056
impl PyDictValues {}
10271057
impl Unconstructible for PyDictValues {}
10281058

0 commit comments

Comments
 (0)