@@ -623,11 +623,7 @@ impl PyContext {
623
623
}
624
624
625
625
pub fn new_instance ( & self , class : PyObjectRef , dict : Option < PyAttributes > ) -> PyObjectRef {
626
- let dict = if let Some ( dict) = dict {
627
- dict
628
- } else {
629
- PyAttributes :: new ( )
630
- } ;
626
+ let dict = dict. unwrap_or_default ( ) ;
631
627
PyObject {
632
628
typ : class,
633
629
dict : Some ( RefCell :: new ( dict) ) ,
@@ -697,7 +693,7 @@ impl Default for PyContext {
697
693
pub struct PyObject {
698
694
pub typ : PyObjectRef ,
699
695
pub dict : Option < RefCell < PyAttributes > > , // __dict__ member
700
- pub payload : Box < dyn Any > ,
696
+ pub payload : Box < dyn PyObjectPayload > ,
701
697
}
702
698
703
699
/// A reference to a Python object.
@@ -1557,7 +1553,7 @@ impl PyValue for PyIteratorValue {
1557
1553
}
1558
1554
1559
1555
impl PyObject {
1560
- pub fn new < T : PyValue > ( payload : T , typ : PyObjectRef ) -> PyObjectRef {
1556
+ pub fn new < T : PyObjectPayload > ( payload : T , typ : PyObjectRef ) -> PyObjectRef {
1561
1557
PyObject {
1562
1558
typ,
1563
1559
dict : Some ( RefCell :: new ( PyAttributes :: new ( ) ) ) ,
@@ -1571,14 +1567,13 @@ impl PyObject {
1571
1567
Rc :: new ( self )
1572
1568
}
1573
1569
1570
+ #[ inline]
1574
1571
pub fn payload < T : PyValue > ( & self ) -> Option < & T > {
1575
- self . payload . downcast_ref ( )
1572
+ self . payload . as_any ( ) . downcast_ref ( )
1576
1573
}
1577
1574
}
1578
1575
1579
- // The intention is for this to replace `PyObjectPayload` once everything is
1580
- // converted to use `PyObjectPayload::AnyRustvalue`.
1581
- pub trait PyValue : Any + fmt:: Debug + Sized {
1576
+ pub trait PyValue : fmt:: Debug + Sized + ' static {
1582
1577
fn required_type ( ctx : & PyContext ) -> PyObjectRef ;
1583
1578
1584
1579
fn into_ref ( self , ctx : & PyContext ) -> PyRef < Self > {
@@ -1603,6 +1598,17 @@ pub trait PyValue: Any + fmt::Debug + Sized {
1603
1598
}
1604
1599
}
1605
1600
1601
+ pub trait PyObjectPayload : Any + fmt:: Debug + ' static {
1602
+ fn as_any ( & self ) -> & dyn Any ;
1603
+ }
1604
+
1605
+ impl < T : PyValue + ' static > PyObjectPayload for T {
1606
+ #[ inline]
1607
+ fn as_any ( & self ) -> & dyn Any {
1608
+ self
1609
+ }
1610
+ }
1611
+
1606
1612
impl FromPyObjectRef for PyRef < PyClass > {
1607
1613
fn from_pyobj ( obj : & PyObjectRef ) -> Self {
1608
1614
if let Some ( _) = obj. payload :: < PyClass > ( ) {
0 commit comments