@@ -591,11 +591,7 @@ impl PyContext {
591
591
}
592
592
593
593
pub fn new_instance ( & self , class : PyObjectRef , dict : Option < PyAttributes > ) -> PyObjectRef {
594
- let dict = if let Some ( dict) = dict {
595
- dict
596
- } else {
597
- PyAttributes :: new ( )
598
- } ;
594
+ let dict = dict. unwrap_or_default ( ) ;
599
595
PyObject {
600
596
typ : Some ( class) ,
601
597
dict : Some ( RefCell :: new ( dict) ) ,
@@ -665,7 +661,7 @@ impl Default for PyContext {
665
661
pub struct PyObject {
666
662
pub typ : Option < PyObjectRef > ,
667
663
pub dict : Option < RefCell < PyAttributes > > , // __dict__ member
668
- pub payload : Box < dyn Any > ,
664
+ pub payload : Box < dyn PyValuePayload > ,
669
665
}
670
666
671
667
impl Default for PyObject {
@@ -1526,7 +1522,7 @@ impl PyValue for PyIteratorValue {
1526
1522
}
1527
1523
1528
1524
impl PyObject {
1529
- pub fn new < T : PyValue > ( payload : T , typ : PyObjectRef ) -> PyObjectRef {
1525
+ pub fn new < T : PyValuePayload > ( payload : T , typ : PyObjectRef ) -> PyObjectRef {
1530
1526
PyObject {
1531
1527
typ : Some ( typ) ,
1532
1528
dict : Some ( RefCell :: new ( PyAttributes :: new ( ) ) ) ,
@@ -1541,16 +1537,31 @@ impl PyObject {
1541
1537
}
1542
1538
1543
1539
pub fn payload < T : PyValue > ( & self ) -> Option < & T > {
1544
- self . payload . downcast_ref ( )
1540
+ let payload: & dyn Any = & self . payload ;
1541
+ payload. downcast_ref ( )
1545
1542
}
1546
1543
}
1547
1544
1548
- // The intention is for this to replace `PyObjectPayload` once everything is
1549
- // converted to use `PyObjectPayload::AnyRustvalue`.
1550
- pub trait PyValue : Any + fmt:: Debug {
1545
+ pub trait PyValue : fmt:: Debug + ' static {
1551
1546
fn required_type ( ctx : & PyContext ) -> PyObjectRef ;
1552
1547
}
1553
1548
1549
+ pub trait PyValuePayload : Any + fmt:: Debug + ' static {
1550
+ fn required_type ( & self , ctx : & PyContext ) -> PyObjectRef ;
1551
+ }
1552
+
1553
+ impl < T : PyValue + ' static > PyValuePayload for T {
1554
+ fn required_type ( & self , ctx : & PyContext ) -> PyObjectRef {
1555
+ T :: required_type ( ctx)
1556
+ }
1557
+ }
1558
+
1559
+ impl PyValuePayload for ( ) {
1560
+ fn required_type ( & self , _ctx : & PyContext ) -> PyObjectRef {
1561
+ panic ! ( "No specific python type for rust unit, don't type check" )
1562
+ }
1563
+ }
1564
+
1554
1565
impl FromPyObjectRef for PyRef < PyClass > {
1555
1566
fn from_pyobj ( obj : & PyObjectRef ) -> Self {
1556
1567
if let Some ( _) = obj. payload :: < PyClass > ( ) {
0 commit comments