Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify IntoInitializer #1

Merged
merged 1 commit into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ struct SubSubClass {
#[pymethods]
impl SubSubClass {
#[new]
fn new() -> impl IntoInitializer<Self> {
Ok(SubClass::new()
.into_initializer()?
.add_subclass(SubSubClass{val3: 20}))
fn new() -> PyClassInitializer<Self> {
SubClass::new()
.into_initializer()
.add_subclass(SubSubClass{val3: 20})
}

fn method3(self_: &PyClassShell<Self>) -> PyResult<usize> {
Expand Down
4 changes: 2 additions & 2 deletions pyo3-derive-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn impl_wrap_new(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
let body = impl_arg_params_(
spec,
cb,
quote! { pyo3::pyclass_init::IntoInitializer::into_initializer },
quote! { pyo3::derive_utils::IntoPyNewResult::into_pynew_result },
);

quote! {
Expand All @@ -174,7 +174,7 @@ pub fn impl_wrap_new(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {

#body

match _result.and_then(|init| init.create_shell(_py)) {
match _result.and_then(|init| init.into_initializer().create_shell(_py)) {
Ok(slf) => slf as _,
Err(e) => e.restore_and_null(_py),
}
Expand Down
21 changes: 21 additions & 0 deletions src/derive_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::init_once;
use crate::instance::PyNativeType;
use crate::types::{PyAny, PyDict, PyModule, PyTuple};
use crate::{ffi, GILPool, IntoPy, PyObject, Python};
use crate::pyclass_init::IntoInitializer;
use std::ptr;

/// Description of a python parameter; used for `parse_args()`.
Expand Down Expand Up @@ -179,3 +180,23 @@ impl<T: IntoPy<PyObject>> IntoPyResult<T> for PyResult<T> {
self
}
}

use crate::pyclass::PyClass;

/// Variant of IntoPyResult for the specific case of #[new]. In the case of returning (Sub, Base)
/// from #[new], IntoPyResult can't apply because (Sub, Base) doesn't implement IntoPy<PyObject>.
pub trait IntoPyNewResult<T: PyClass, I: IntoInitializer<T>> {
fn into_pynew_result(self) -> PyResult<I>;
}

impl<T: PyClass, I: IntoInitializer<T>> IntoPyNewResult<T, I> for I {
fn into_pynew_result(self) -> PyResult<I> {
Ok(self)
}
}

impl<T: PyClass, I: IntoInitializer<T>> IntoPyNewResult<T, I> for PyResult<I> {
fn into_pynew_result(self) -> PyResult<I> {
self
}
}
2 changes: 1 addition & 1 deletion src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<T> Py<T> {
<T::BaseType as PyTypeInfo>::ConcreteLayout:
crate::type_object::PyObjectSizedLayout<T::BaseType>,
{
let initializer = value.into_initializer()?;
let initializer = value.into_initializer();
let obj = unsafe { initializer.create_shell(py)? };
let ob = unsafe { Py::from_owned_ptr(obj as _) };
Ok(ob)
Expand Down
4 changes: 2 additions & 2 deletions src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<T: PyClass> PyClassShell<T> {
crate::type_object::PyObjectSizedLayout<T::BaseType>,
{
unsafe {
let initializer = value.into_initializer()?;
let initializer = value.into_initializer();
let self_ = initializer.create_shell(py)?;
FromPyPointer::from_owned_ptr_or_err(py, self_ as _)
}
Expand All @@ -149,7 +149,7 @@ impl<T: PyClass> PyClassShell<T> {
crate::type_object::PyObjectSizedLayout<T::BaseType>,
{
unsafe {
let initializer = value.into_initializer()?;
let initializer = value.into_initializer();
let self_ = initializer.create_shell(py)?;
FromPyPointer::from_owned_ptr_or_err(py, self_ as _)
}
Expand Down
60 changes: 20 additions & 40 deletions src/pyclass_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,64 +109,44 @@ impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T> {
}
}

/// Represets that we can convert the type to `PyClassInitializer`.
///
/// It is mainly used in our proc-macro code.
pub trait IntoInitializer<T: PyClass> {
fn into_initializer(self) -> PyResult<PyClassInitializer<T>>;
}

impl<T> IntoInitializer<T> for T
impl<T> From<T> for PyClassInitializer<T>
where
T: PyClass,
T::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<T::BaseType>>,
{
fn into_initializer(self) -> PyResult<PyClassInitializer<T>> {
Ok(PyClassInitializer::from_value(self))
fn from(value: T) -> PyClassInitializer<T> {
PyClassInitializer::from_value(value)
}
}

impl<T> IntoInitializer<T> for PyResult<T>
where
T: PyClass,
T::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<T::BaseType>>,
{
fn into_initializer(self) -> PyResult<PyClassInitializer<T>> {
self.map(PyClassInitializer::from_value)
}
/// An extension of Into which extends the range of possible types from `#[pyclass]`'s `#[new]`.
///
/// In particular it permits for the return type of `#[new]` to be a (SubType, BaseType) pair
/// which will also be initialized.
///
/// It is mainly used in our proc-macro code.
pub trait IntoInitializer<T: PyClass> {
fn into_initializer(self) -> PyClassInitializer<T>;
}

impl<S, B> IntoInitializer<S> for (S, B)
impl<T, U> IntoInitializer<T> for U
where
S: PyClass + PyTypeInfo<BaseType = B>,
B: PyClass + PyTypeInfo<Initializer = PyClassInitializer<B>>,
B::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<B::BaseType>>,
T: PyClass,
U: Into<PyClassInitializer<T>>
{
fn into_initializer(self) -> PyResult<PyClassInitializer<S>> {
let (sub, base) = self;
Ok(PyClassInitializer::from_value(base).add_subclass(sub))
fn into_initializer(self) -> PyClassInitializer<T> {
self.into()
}
}

impl<S, B> IntoInitializer<S> for PyResult<(S, B)>
impl<S, B> IntoInitializer<S> for (S, B)
where
S: PyClass + PyTypeInfo<BaseType = B>,
B: PyClass + PyTypeInfo<Initializer = PyClassInitializer<B>>,
B::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<B::BaseType>>,
{
fn into_initializer(self) -> PyResult<PyClassInitializer<S>> {
self.map(|(sub, base)| PyClassInitializer::from_value(base).add_subclass(sub))
}
}

impl<T: PyClass> IntoInitializer<T> for PyClassInitializer<T> {
fn into_initializer(self) -> PyResult<PyClassInitializer<T>> {
Ok(self)
}
}

impl<T: PyClass> IntoInitializer<T> for PyResult<PyClassInitializer<T>> {
fn into_initializer(self) -> PyResult<PyClassInitializer<T>> {
self
fn into_initializer(self) -> PyClassInitializer<S> {
let (sub, base_init) = self;
base_init.into_initializer().add_subclass(sub)
}
}