@@ -32,10 +32,15 @@ use pyo3::prelude::*;
3232macro_rules! impl_into_py_for_enum {
3333 ( $Enum: ident; $( $Variant: ident $( , ) ?) * ) => {
3434 #[ cfg( feature = "pylib" ) ]
35- impl IntoPy <PyObject > for $Enum {
36- fn into_py( self , py: Python <' _>) -> PyObject {
35+ impl <' py> IntoPyObject <' py> for $Enum {
36+ type Target = pyo3:: PyAny ;
37+ type Output = pyo3:: Bound <' py, Self :: Target >;
38+ type Error = pyo3:: PyErr ;
39+ fn into_pyobject( self , py: pyo3:: Python <' py>) -> Result <Self :: Output , Self :: Error > {
40+ use pyo3:: IntoPyObjectExt ;
41+
3742 match self {
38- $( Self :: $Variant( v) => v . into_py ( py) , ) *
43+ $( Self :: $Variant( v) => Ok ( v . into_py_any ( py) ? . into_bound ( py ) ) , ) *
3944 }
4045 }
4146 }
@@ -46,7 +51,7 @@ macro_rules! impl_from_py_for_enum {
4651 ( $Ty: ty; $( $Variant: ident ( $inner: ident) $( , ) * ) * ) => {
4752 #[ cfg( feature = "pylib" ) ]
4853 impl FromPyObject <' _> for $Ty {
49- fn extract ( ob: & PyAny ) -> PyResult <Self > {
54+ fn extract_bound ( ob: & pyo3 :: Bound < ' _ , PyAny > ) -> PyResult <Self > {
5055 $( if let Ok ( extracted) = ob. extract:: <$inner>( ) {
5156 return Ok ( Self :: $Variant( extracted) ) ;
5257 } else) * {
@@ -60,7 +65,7 @@ macro_rules! impl_from_py_for_enum {
6065 ( $Ty: ty; $( $Variant: ident $( , ) * ) * ) => {
6166 #[ cfg( feature = "pylib" ) ]
6267 impl FromPyObject <' _> for $Ty {
63- fn extract ( ob: & PyAny ) -> PyResult <Self > {
68+ fn extract_bound ( ob: & pyo3 :: Bound < ' _ , PyAny > ) -> PyResult <Self > {
6469 $( if let Ok ( extracted) = ob. extract:: <$Variant>( ) {
6570 return Ok ( Self :: $Variant( extracted) ) ;
6671 } else) * {
@@ -791,18 +796,23 @@ pub enum TypeAppArgsKind {
791796}
792797
793798#[ cfg( feature = "pylib" ) ]
794- impl IntoPy < PyObject > for TypeAppArgsKind {
795- fn into_py ( self , py : Python < ' _ > ) -> PyObject {
799+ impl < ' py > IntoPyObject < ' py > for TypeAppArgsKind {
800+ type Target = PyAny ;
801+ type Output = Bound < ' py , Self :: Target > ;
802+ type Error = PyErr ;
803+ fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
804+ use pyo3:: IntoPyObjectExt ;
805+
796806 match self {
797- Self :: SubtypeOf ( ty) => ty. into_py ( py) ,
798- Self :: Args ( args) => args. into_py ( py) ,
807+ Self :: SubtypeOf ( ty) => Ok ( ty. into_py_any ( py) ? . into_bound ( py ) ) ,
808+ Self :: Args ( args) => Ok ( args. into_py_any ( py) ? . into_bound ( py ) ) ,
799809 }
800810 }
801811}
802812
803813#[ cfg( feature = "pylib" ) ]
804814impl FromPyObject < ' _ > for TypeAppArgsKind {
805- fn extract ( ob : & PyAny ) -> PyResult < Self > {
815+ fn extract_bound ( ob : & Bound < ' _ , PyAny > ) -> PyResult < Self > {
806816 if let Ok ( ty) = ob. extract :: < TypeSpecWithOp > ( ) {
807817 Ok ( Self :: SubtypeOf ( Box :: new ( ty) ) )
808818 } else if let Ok ( args) = ob. extract :: < Args > ( ) {
@@ -4146,9 +4156,12 @@ pub enum TypeSpec {
41464156
41474157// TODO:
41484158#[ cfg( feature = "pylib" ) ]
4149- impl IntoPy < PyObject > for TypeSpec {
4150- fn into_py ( self , py : Python < ' _ > ) -> PyObject {
4151- pyo3:: types:: PyNone :: get_bound ( py) . to_object ( py)
4159+ impl < ' py > IntoPyObject < ' py > for TypeSpec {
4160+ type Target = pyo3:: types:: PyNone ;
4161+ type Output = Borrowed < ' py , ' py , Self :: Target > ;
4162+ type Error = std:: convert:: Infallible ;
4163+ fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
4164+ pyo3:: types:: PyNone :: get ( py) . into_pyobject ( py)
41524165 }
41534166}
41544167
@@ -4620,14 +4633,19 @@ impl Hash for VisModifierSpec {
46204633}
46214634
46224635#[ cfg( feature = "pylib" ) ]
4623- impl IntoPy < PyObject > for VisModifierSpec {
4624- fn into_py ( self , py : Python < ' _ > ) -> PyObject {
4636+ impl < ' py > IntoPyObject < ' py > for VisModifierSpec {
4637+ type Target = PyAny ;
4638+ type Output = Bound < ' py , PyAny > ;
4639+ type Error = PyErr ;
4640+
4641+ fn into_pyobject ( self , py : Python < ' py > ) -> PyResult < Self :: Output > {
4642+ use pyo3:: IntoPyObjectExt ;
4643+
46254644 match self {
4626- Self :: Private => py. None ( ) ,
4627- Self :: Auto => py. None ( ) ,
4628- Self :: Public ( token) => token. into_py ( py) ,
4629- Self :: ExplicitPrivate ( token) => token. into_py ( py) ,
4630- Self :: Restricted ( rest) => rest. into_py ( py) ,
4645+ Self :: Private | Self :: Auto => py. None ( ) . into_bound_py_any ( py) ,
4646+ Self :: Public ( token) => token. into_bound_py_any ( py) ,
4647+ Self :: ExplicitPrivate ( token) => token. into_bound_py_any ( py) ,
4648+ Self :: Restricted ( rest) => rest. into_bound_py_any ( py) ,
46314649 }
46324650 }
46334651}
0 commit comments