@@ -4,15 +4,16 @@ use crate::{
44 common:: ascii,
55 dictdatatype:: { self , DictKey } ,
66 function:: { ArgIterable , FuncArgs , KwArgs , OptionalArg } ,
7- protocol:: PyIterReturn ,
7+ protocol:: { PyIterReturn , PyMappingMethods } ,
88 slots:: {
9- Comparable , Hashable , Iterable , IteratorIterable , PyComparisonOp , SlotIterator , Unhashable ,
9+ AsMapping , Comparable , Hashable , Iterable , IteratorIterable , PyComparisonOp , SlotIterator ,
10+ Unhashable ,
1011 } ,
1112 vm:: { ReprGuard , VirtualMachine } ,
1213 IdProtocol , IntoPyObject , ItemProtocol ,
1314 PyArithmeticValue :: * ,
1415 PyAttributes , PyClassDef , PyClassImpl , PyComparisonValue , PyContext , PyObjectRef , PyRef ,
15- PyResult , PyValue , TryFromObject , TypeProtocol ,
16+ PyResult , PyValue , TypeProtocol ,
1617} ;
1718use rustpython_common:: lock:: PyMutex ;
1819use std:: fmt;
@@ -51,7 +52,7 @@ impl PyValue for PyDict {
5152
5253// Python dict methods:
5354#[ allow( clippy:: len_without_is_empty) ]
54- #[ pyimpl( with( Hashable , Comparable , Iterable ) , flags( BASETYPE ) ) ]
55+ #[ pyimpl( with( AsMapping , Hashable , Comparable , Iterable ) , flags( BASETYPE ) ) ]
5556impl PyDict {
5657 #[ pyslot]
5758 fn slot_new ( cls : PyTypeRef , _args : FuncArgs , vm : & VirtualMachine ) -> PyResult {
@@ -410,6 +411,39 @@ impl PyDict {
410411 }
411412}
412413
414+ impl AsMapping for PyDict {
415+ fn as_mapping ( _zelf : & PyRef < Self > , _vm : & VirtualMachine ) -> PyResult < PyMappingMethods > {
416+ Ok ( PyMappingMethods {
417+ length : Some ( Self :: length) ,
418+ subscript : Some ( Self :: subscript) ,
419+ ass_subscript : Some ( Self :: ass_subscript) ,
420+ } )
421+ }
422+
423+ #[ inline]
424+ fn length ( zelf : PyObjectRef , vm : & VirtualMachine ) -> PyResult < usize > {
425+ Self :: downcast_ref ( & zelf, vm) . map ( |zelf| Ok ( zelf. len ( ) ) ) ?
426+ }
427+
428+ #[ inline]
429+ fn subscript ( zelf : PyObjectRef , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
430+ Self :: downcast ( zelf, vm) . map ( |zelf| Self :: getitem ( zelf, needle, vm) ) ?
431+ }
432+
433+ #[ inline]
434+ fn ass_subscript (
435+ zelf : PyObjectRef ,
436+ needle : PyObjectRef ,
437+ value : Option < PyObjectRef > ,
438+ vm : & VirtualMachine ,
439+ ) -> PyResult < ( ) > {
440+ Self :: downcast_ref ( & zelf, vm) . map ( |zelf| match value {
441+ Some ( value) => zelf. setitem ( needle, value, vm) ,
442+ None => zelf. delitem ( needle, vm) ,
443+ } ) ?
444+ }
445+ }
446+
413447impl Comparable for PyDict {
414448 fn cmp (
415449 zelf : & PyRef < Self > ,
@@ -653,7 +687,7 @@ macro_rules! dict_iterator {
653687 }
654688
655689 impl $name {
656- fn new( dict: PyDictRef ) -> Self {
690+ pub fn new( dict: PyDictRef ) -> Self {
657691 $name { dict }
658692 }
659693 }
@@ -897,26 +931,3 @@ pub(crate) fn init(context: &PyContext) {
897931 PyDictItemIterator :: extend_class ( context, & context. types . dict_itemiterator_type ) ;
898932 PyDictReverseItemIterator :: extend_class ( context, & context. types . dict_reverseitemiterator_type ) ;
899933}
900-
901- pub struct PyMapping {
902- dict : PyDictRef ,
903- }
904-
905- impl TryFromObject for PyMapping {
906- fn try_from_object ( vm : & VirtualMachine , obj : PyObjectRef ) -> PyResult < Self > {
907- let dict = vm. ctx . new_dict ( ) ;
908- PyDict :: merge (
909- & dict. entries ,
910- OptionalArg :: Present ( obj) ,
911- KwArgs :: default ( ) ,
912- vm,
913- ) ?;
914- Ok ( PyMapping { dict } )
915- }
916- }
917-
918- impl PyMapping {
919- pub fn into_dict ( self ) -> PyDictRef {
920- self . dict
921- }
922- }
0 commit comments