impl sequence protocol for dict_items dict_values dict_keys
@@ -999,7 +999,7 @@ trait ViewSetOps: DictView {
}
impl ViewSetOps for PyDictKeys {}
-#[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))]
+#[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps, AsSequence))]
impl PyDictKeys {
#[pymethod(magic)]
fn contains(zelf: PyRef<Self>, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
@@ -1019,8 +1019,24 @@ impl Comparable for PyDictKeys {
}
}
+impl AsSequence for PyDictKeys {
+ fn as_sequence(
+ _zelf: &PyObjectView<Self>,
+ _vm: &VirtualMachine,
+ ) -> Cow<'static, PySequenceMethods> {
+ Cow::Borrowed(&Self::SEQUENCE_METHODS)
+ }
+}
+impl PyDictKeys {
+ const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
+ length: Some(|seq, _vm| Ok(seq.obj_as::<Self>().len())),
+ contains: Some(|seq, target, vm| seq.obj_as::<Self>().dict.entries.contains(vm, target)),
+ ..*PySequenceMethods::not_implemented()
+ };
+}
+
impl ViewSetOps for PyDictItems {}
-#[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))]
+#[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps, AsSequence))]
impl PyDictItems {
#[pymethod(magic)]
fn contains(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
@@ -1055,10 +1071,41 @@ impl Comparable for PyDictItems {
}
}
-#[pyimpl(with(DictView, Constructor, Iterable))]
+impl AsSequence for PyDictItems {
+ fn as_sequence(
+ _zelf: &PyObjectView<Self>,
+ _vm: &VirtualMachine,
+ ) -> Cow<'static, PySequenceMethods> {
+ Cow::Borrowed(&Self::SEQUENCE_METHODS)
+ }
+}
+impl PyDictItems {
+ const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
+ length: Some(|seq, _vm| Ok(seq.obj_as::<Self>().len())),
+ contains: Some(|seq, target, vm| seq.obj_as::<Self>().dict.entries.contains(vm, target)),
+ ..*PySequenceMethods::not_implemented()
+ };
+}
+
+#[pyimpl(with(DictView, Constructor, Iterable, AsSequence))]
impl PyDictValues {}
impl Unconstructible for PyDictValues {}
+impl AsSequence for PyDictValues {
+ fn as_sequence(
+ _zelf: &PyObjectView<Self>,
+ _vm: &VirtualMachine,
+ ) -> Cow<'static, PySequenceMethods> {
+ Cow::Borrowed(&Self::SEQUENCE_METHODS)
+ }
+}
+impl PyDictValues {
+ const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
+ length: Some(|seq, _vm| Ok(seq.obj_as::<Self>().len())),
+ ..*PySequenceMethods::not_implemented()
+ };
+}
+
pub(crate) fn init(context: &PyContext) {
PyDict::extend_class(context, &context.types.dict_type);
PyDictKeys::extend_class(context, &context.types.dict_keys_type);
