File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1578,8 +1578,29 @@ impl PyObject {
15781578
15791579// The intention is for this to replace `PyObjectPayload` once everything is
15801580// converted to use `PyObjectPayload::AnyRustvalue`.
1581- pub trait PyValue : Any + fmt:: Debug {
1581+ pub trait PyValue : Any + fmt:: Debug + Sized {
15821582 fn required_type ( ctx : & PyContext ) -> PyObjectRef ;
1583+
1584+ fn into_ref ( self , ctx : & PyContext ) -> PyRef < Self > {
1585+ PyRef {
1586+ obj : PyObject :: new ( self , Self :: required_type ( ctx) ) ,
1587+ _payload : PhantomData ,
1588+ }
1589+ }
1590+
1591+ fn into_ref_with_type ( self , vm : & mut VirtualMachine , cls : PyClassRef ) -> PyResult < PyRef < Self > > {
1592+ let required_type = Self :: required_type ( & vm. ctx ) ;
1593+ if objtype:: issubclass ( & cls. obj , & required_type) {
1594+ Ok ( PyRef {
1595+ obj : PyObject :: new ( self , cls. obj ) ,
1596+ _payload : PhantomData ,
1597+ } )
1598+ } else {
1599+ let subtype = vm. to_pystr ( & cls. obj ) ?;
1600+ let basetype = vm. to_pystr ( & required_type) ?;
1601+ Err ( vm. new_type_error ( format ! ( "{} is not a subtype of {}" , subtype, basetype) ) )
1602+ }
1603+ }
15831604}
15841605
15851606impl FromPyObjectRef for PyRef < PyClass > {
You can’t perform that action at this time.
0 commit comments