@@ -3,7 +3,7 @@ use std::collections::HashMap;
33use std:: fmt;
44use std:: iter;
55use std:: ops:: RangeInclusive ;
6- use std:: rc:: { Rc , Weak } ;
6+ use std:: rc:: Rc ;
77
88use num_bigint:: BigInt ;
99use num_bigint:: ToBigInt ;
@@ -43,6 +43,7 @@ use crate::obj::objstr;
4343use crate :: obj:: objsuper;
4444use crate :: obj:: objtuple:: { self , PyTuple } ;
4545use crate :: obj:: objtype:: { self , PyClass } ;
46+ use crate :: obj:: objweakref;
4647use crate :: obj:: objzip;
4748use crate :: vm:: VirtualMachine ;
4849
@@ -67,9 +68,6 @@ Basically reference counting, but then done by rust.
6768/// to the python object by 1.
6869pub type PyObjectRef = Rc < PyObject > ;
6970
70- /// Same as PyObjectRef, except for being a weak reference.
71- pub type PyObjectWeakRef = Weak < PyObject > ;
72-
7371/// Use this type for function which return a python object or and exception.
7472/// Both the python object and the python exception are `PyObjectRef` types
7573/// since exceptions are also python objects.
@@ -141,6 +139,7 @@ pub struct PyContext {
141139 pub readonly_property_type : PyObjectRef ,
142140 pub module_type : PyObjectRef ,
143141 pub bound_method_type : PyObjectRef ,
142+ pub weakref_type : PyObjectRef ,
144143 pub object : PyObjectRef ,
145144 pub exceptions : exceptions:: ExceptionZoo ,
146145}
@@ -185,6 +184,7 @@ impl PyContext {
185184 let readonly_property_type =
186185 create_type ( "readonly_property" , & type_type, & object_type, & dict_type) ;
187186 let super_type = create_type ( "super" , & type_type, & object_type, & dict_type) ;
187+ let weakref_type = create_type ( "ref" , & type_type, & object_type, & dict_type) ;
188188 let generator_type = create_type ( "generator" , & type_type, & object_type, & dict_type) ;
189189 let bound_method_type = create_type ( "method" , & type_type, & object_type, & dict_type) ;
190190 let str_type = create_type ( "str" , & type_type, & object_type, & dict_type) ;
@@ -284,6 +284,7 @@ impl PyContext {
284284 generator_type,
285285 module_type,
286286 bound_method_type,
287+ weakref_type,
287288 type_type,
288289 exceptions,
289290 } ;
@@ -316,6 +317,7 @@ impl PyContext {
316317 objbool:: init ( & context) ;
317318 objcode:: init ( & context) ;
318319 objframe:: init ( & context) ;
320+ objweakref:: init ( & context) ;
319321 objnone:: init ( & context) ;
320322 objmodule:: init ( & context) ;
321323 exceptions:: init ( & context) ;
@@ -450,6 +452,10 @@ impl PyContext {
450452 self . bound_method_type . clone ( )
451453 }
452454
455+ pub fn weakref_type ( & self ) -> PyObjectRef {
456+ self . weakref_type . clone ( )
457+ }
458+
453459 pub fn type_type ( & self ) -> PyObjectRef {
454460 self . type_type . clone ( )
455461 }
@@ -465,6 +471,7 @@ impl PyContext {
465471 pub fn not_implemented ( & self ) -> PyObjectRef {
466472 self . not_implemented . clone ( )
467473 }
474+
468475 pub fn object ( & self ) -> PyObjectRef {
469476 self . object . clone ( )
470477 }
@@ -1481,7 +1488,6 @@ into_py_native_func_tuple!((a, A), (b, B), (c, C), (d, D), (e, E));
14811488/// of rust data for a particular python object. Determine the python type
14821489/// by using for example the `.typ()` method on a python object.
14831490pub enum PyObjectPayload {
1484- WeakRef { referent : PyObjectWeakRef } ,
14851491 AnyRustValue { value : Box < dyn std:: any:: Any > } ,
14861492}
14871493
@@ -1510,7 +1516,6 @@ impl PyObjectPayload2 for PyIteratorValue {
15101516impl fmt:: Debug for PyObjectPayload {
15111517 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
15121518 match self {
1513- PyObjectPayload :: WeakRef { .. } => write ! ( f, "weakref" ) ,
15141519 PyObjectPayload :: AnyRustValue { value } => value. fmt ( f) ,
15151520 }
15161521 }
@@ -1532,11 +1537,8 @@ impl PyObject {
15321537 }
15331538
15341539 pub fn payload < T : PyObjectPayload2 > ( & self ) -> Option < & T > {
1535- if let PyObjectPayload :: AnyRustValue { ref value } = self . payload {
1536- value. downcast_ref ( )
1537- } else {
1538- None
1539- }
1540+ let PyObjectPayload :: AnyRustValue { ref value } = self . payload ;
1541+ value. downcast_ref ( )
15401542 }
15411543}
15421544
0 commit comments