Skip to content

Commit

Permalink
macro impls
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Jul 6, 2024
1 parent 815deb3 commit 1b1dd3b
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,10 +941,39 @@ fn impl_complex_enum(
}
};

let enum_into_pyobject_impl = {
let match_arms = variants
.iter()
.map(|variant| {
let variant_ident = variant.get_ident();
let variant_cls = gen_complex_enum_variant_class_ident(cls, variant.get_ident());
quote! {
#cls::#variant_ident { .. } => {
let pyclass_init = #pyo3_path::PyClassInitializer::from(self).add_subclass(#variant_cls);
unsafe { #pyo3_path::Bound::new(py, pyclass_init).map(|b| #pyo3_path::types::PyAnyMethods::downcast_into_unchecked(b.into_any())) }
}
}
});

quote! {
impl<'py> #pyo3_path::conversion::IntoPyObject<'py> for #cls {
type Target = Self;
type Error = #pyo3_path::PyErr;

fn into_pyobject(self, py: #pyo3_path::Python<'py>) -> ::std::result::Result<#pyo3_path::Bound<'py, Self::Target>, Self::Error> {
match self {
#(#match_arms)*
}
}
}
}
};

let pyclass_impls: TokenStream = [
impl_builder.impl_pyclass(ctx),
impl_builder.impl_extractext(ctx),
enum_into_py_impl,
enum_into_pyobject_impl,
impl_builder.impl_pyclassimpl(ctx)?,
impl_builder.impl_add_to_module(ctx),
impl_builder.impl_freelist(ctx),
Expand Down Expand Up @@ -2013,6 +2042,15 @@ impl<'a> PyClassImplsBuilder<'a> {
#pyo3_path::IntoPy::into_py(#pyo3_path::Py::new(py, self).unwrap(), py)
}
}

impl<'py> #pyo3_path::conversion::IntoPyObject<'py> for #cls {
type Target = Self;
type Error = #pyo3_path::PyErr;

fn into_pyobject(self, py: #pyo3_path::Python<'py>) -> ::std::result::Result<#pyo3_path::Bound<'py, Self::Target>, Self::Error> {
#pyo3_path::Bound::new(py, self)
}
}
}
} else {
quote! {}
Expand Down

0 comments on commit 1b1dd3b

Please sign in to comment.