@@ -655,12 +655,12 @@ impl PyContext {
655655 } else {
656656 PyAttributes :: new ( )
657657 } ;
658- PyObject :: new (
659- PyObjectPayload :: Instance {
660- dict : RefCell :: new ( dict ) ,
661- } ,
662- class ,
663- )
658+ PyObject {
659+ payload : PyObjectPayload :: NoPayload ,
660+ typ : Some ( class ) ,
661+ dict : Some ( RefCell :: new ( dict ) ) ,
662+ }
663+ . into_ref ( )
664664 }
665665
666666 // Item set/get:
@@ -682,14 +682,12 @@ impl PyContext {
682682 }
683683
684684 pub fn set_attr ( & self , obj : & PyObjectRef , attr_name : & str , value : PyObjectRef ) {
685- match obj. payload {
686- PyObjectPayload :: Module { ref scope, .. } => {
687- scope. locals . set_item ( self , attr_name, value)
688- }
689- PyObjectPayload :: Instance { ref dict } | PyObjectPayload :: Class { ref dict, .. } => {
690- dict. borrow_mut ( ) . insert ( attr_name. to_string ( ) , value) ;
691- }
692- ref payload => unimplemented ! ( "set_attr unimplemented for: {:?}" , payload) ,
685+ if let PyObjectPayload :: Module { ref scope, .. } = obj. payload {
686+ scope. locals . set_item ( self , attr_name, value)
687+ } else if let Some ( ref dict) = obj. dict {
688+ dict. borrow_mut ( ) . insert ( attr_name. to_string ( ) , value) ;
689+ } else {
690+ unimplemented ! ( "set_attr unimplemented for: {:?}" , obj) ;
693691 } ;
694692 }
695693
@@ -728,7 +726,7 @@ impl Default for PyContext {
728726pub struct PyObject {
729727 pub payload : PyObjectPayload ,
730728 pub typ : Option < PyObjectRef > ,
731- pub dict : Option < HashMap < String , PyObjectRef > > , // __dict__ member
729+ pub dict : Option < RefCell < PyAttributes > > , // __dict__ member
732730}
733731
734732pub trait IdProtocol {
@@ -775,16 +773,18 @@ pub trait AttributeProtocol {
775773}
776774
777775fn class_get_item ( class : & PyObjectRef , attr_name : & str ) -> Option < PyObjectRef > {
778- match class. payload {
779- PyObjectPayload :: Class { ref dict, .. } => dict. borrow ( ) . get ( attr_name) . cloned ( ) ,
780- _ => panic ! ( "Only classes should be in MRO!" ) ,
776+ if let Some ( ref dict) = class. dict {
777+ dict. borrow ( ) . get ( attr_name) . cloned ( )
778+ } else {
779+ panic ! ( "Only classes should be in MRO!" ) ;
781780 }
782781}
783782
784783fn class_has_item ( class : & PyObjectRef , attr_name : & str ) -> bool {
785- match class. payload {
786- PyObjectPayload :: Class { ref dict, .. } => dict. borrow ( ) . contains_key ( attr_name) ,
787- _ => panic ! ( "Only classes should be in MRO!" ) ,
784+ if let Some ( ref dict) = class. dict {
785+ dict. borrow ( ) . contains_key ( attr_name)
786+ } else {
787+ panic ! ( "Only classes should be in MRO!" ) ;
788788 }
789789}
790790
@@ -803,8 +803,13 @@ impl AttributeProtocol for PyObjectRef {
803803 }
804804 None
805805 }
806- PyObjectPayload :: Instance { ref dict } => dict. borrow ( ) . get ( attr_name) . cloned ( ) ,
807- _ => None ,
806+ _ => {
807+ if let Some ( ref dict) = self . dict {
808+ dict. borrow ( ) . get ( attr_name) . cloned ( )
809+ } else {
810+ None
811+ }
812+ }
808813 }
809814 }
810815
@@ -814,8 +819,13 @@ impl AttributeProtocol for PyObjectRef {
814819 PyObjectPayload :: Class { ref mro, .. } => {
815820 class_has_item ( self , attr_name) || mro. iter ( ) . any ( |d| class_has_item ( d, attr_name) )
816821 }
817- PyObjectPayload :: Instance { ref dict } => dict. borrow ( ) . contains_key ( attr_name) ,
818- _ => false ,
822+ _ => {
823+ if let Some ( ref dict) = self . dict {
824+ dict. borrow ( ) . contains_key ( attr_name)
825+ } else {
826+ false
827+ }
828+ }
819829 }
820830 }
821831}
@@ -1485,7 +1495,6 @@ pub enum PyObjectPayload {
14851495 NoPayload ,
14861496 Class {
14871497 name : String ,
1488- dict : RefCell < PyAttributes > ,
14891498 mro : Vec < PyObjectRef > ,
14901499 } ,
14911500 Set {
@@ -1494,9 +1503,6 @@ pub enum PyObjectPayload {
14941503 WeakRef {
14951504 referent : PyObjectWeakRef ,
14961505 } ,
1497- Instance {
1498- dict : RefCell < PyAttributes > ,
1499- } ,
15001506 RustFunction {
15011507 function : PyNativeFunc ,
15021508 } ,
@@ -1536,7 +1542,6 @@ impl fmt::Debug for PyObjectPayload {
15361542 PyObjectPayload :: Module { .. } => write ! ( f, "module" ) ,
15371543 PyObjectPayload :: NoPayload => write ! ( f, "NoPayload" ) ,
15381544 PyObjectPayload :: Class { ref name, .. } => write ! ( f, "class {:?}" , name) ,
1539- PyObjectPayload :: Instance { .. } => write ! ( f, "instance" ) ,
15401545 PyObjectPayload :: RustFunction { .. } => write ! ( f, "rust function" ) ,
15411546 PyObjectPayload :: Frame { .. } => write ! ( f, "frame" ) ,
15421547 PyObjectPayload :: AnyRustValue { .. } => write ! ( f, "some rust value" ) ,
0 commit comments