2020-01-08 13:44:50 +00:00
|
|
|
//! Initialization utilities for `#[pyclass]`.
|
2021-05-04 20:10:03 +00:00
|
|
|
use crate::type_object::{PyLayout, PyTypeInfo};
|
|
|
|
use crate::{callback::IntoPyCallbackOutput, class::impl_::PyClassBaseType};
|
2020-12-13 22:51:19 +00:00
|
|
|
use crate::{PyCell, PyClass, PyResult, Python};
|
2020-01-05 07:01:05 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2020-01-08 13:44:50 +00:00
|
|
|
/// Initializer for Python types.
|
|
|
|
///
|
|
|
|
/// This trait is intended to use internally for distinguishing `#[pyclass]` and
|
|
|
|
/// Python native types.
|
2021-05-04 20:10:03 +00:00
|
|
|
pub trait PyObjectInit<T>: Sized {
|
2020-02-22 15:56:58 +00:00
|
|
|
fn init_class<L: PyLayout<T>>(self, layout: &mut L);
|
2020-01-08 13:44:50 +00:00
|
|
|
private_decl! {}
|
2020-01-05 07:01:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 13:44:50 +00:00
|
|
|
/// Initializer for Python native type, like `PyDict`.
|
2020-01-05 07:01:05 +00:00
|
|
|
pub struct PyNativeTypeInitializer<T: PyTypeInfo>(PhantomData<T>);
|
|
|
|
|
|
|
|
impl<T: PyTypeInfo> PyObjectInit<T> for PyNativeTypeInitializer<T> {
|
2020-02-22 15:56:58 +00:00
|
|
|
fn init_class<L: PyLayout<T>>(self, _layout: &mut L) {}
|
2020-01-08 13:44:50 +00:00
|
|
|
private_impl! {}
|
2020-01-05 07:01:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 13:44:50 +00:00
|
|
|
/// Initializer for our `#[pyclass]` system.
|
2020-01-05 07:01:05 +00:00
|
|
|
///
|
|
|
|
/// You can use this type to initalize complicatedly nested `#[pyclass]`.
|
|
|
|
///
|
2021-03-20 07:45:56 +00:00
|
|
|
/// # Examples
|
2020-01-08 13:44:50 +00:00
|
|
|
///
|
2020-01-05 07:01:05 +00:00
|
|
|
/// ```
|
|
|
|
/// # use pyo3::prelude::*;
|
|
|
|
/// # use pyo3::py_run;
|
2021-03-20 07:44:28 +00:00
|
|
|
/// # use pyo3::types::IntoPyDict;
|
2020-09-01 00:36:26 +00:00
|
|
|
/// #[pyclass(subclass)]
|
2020-01-05 07:01:05 +00:00
|
|
|
/// struct BaseClass {
|
|
|
|
/// #[pyo3(get)]
|
|
|
|
/// basename: &'static str,
|
|
|
|
/// }
|
2020-09-01 00:36:26 +00:00
|
|
|
/// #[pyclass(extends=BaseClass, subclass)]
|
2020-01-05 07:01:05 +00:00
|
|
|
/// struct SubClass {
|
|
|
|
/// #[pyo3(get)]
|
|
|
|
/// subname: &'static str,
|
|
|
|
/// }
|
|
|
|
/// #[pyclass(extends=SubClass)]
|
|
|
|
/// struct SubSubClass {
|
|
|
|
/// #[pyo3(get)]
|
|
|
|
/// subsubname: &'static str,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[pymethods]
|
|
|
|
/// impl SubSubClass {
|
|
|
|
/// #[new]
|
|
|
|
/// fn new() -> PyClassInitializer<Self> {
|
2020-01-08 13:44:50 +00:00
|
|
|
/// PyClassInitializer::from(BaseClass { basename: "base" })
|
|
|
|
/// .add_subclass(SubClass { subname: "sub" })
|
|
|
|
/// .add_subclass(SubSubClass { subsubname: "subsub" })
|
2020-01-05 07:01:05 +00:00
|
|
|
/// }
|
|
|
|
/// }
|
2021-03-20 07:44:28 +00:00
|
|
|
/// Python::with_gil(|py| {
|
|
|
|
/// let typeobj = py.get_type::<SubSubClass>();
|
|
|
|
/// let sub_sub_class = typeobj.call((), None).unwrap();
|
|
|
|
/// py_run!(
|
|
|
|
/// py,
|
|
|
|
/// sub_sub_class,
|
|
|
|
/// r#"
|
|
|
|
/// assert sub_sub_class.basename == 'base'
|
|
|
|
/// assert sub_sub_class.subname == 'sub'
|
|
|
|
/// assert sub_sub_class.subsubname == 'subsub'"#
|
|
|
|
/// );
|
|
|
|
/// });
|
2020-01-05 07:01:05 +00:00
|
|
|
/// ```
|
|
|
|
pub struct PyClassInitializer<T: PyClass> {
|
|
|
|
init: T,
|
2021-05-04 20:10:03 +00:00
|
|
|
super_init: <T::BaseType as PyClassBaseType>::Initializer,
|
2020-01-05 07:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: PyClass> PyClassInitializer<T> {
|
2020-08-27 11:27:16 +00:00
|
|
|
/// Construct new initializer from value `T` and base class' initializer.
|
2020-01-08 13:44:50 +00:00
|
|
|
///
|
|
|
|
/// We recommend to mainly use `add_subclass`, instead of directly call `new`.
|
2021-05-04 20:10:03 +00:00
|
|
|
pub fn new(init: T, super_init: <T::BaseType as PyClassBaseType>::Initializer) -> Self {
|
2020-01-08 13:44:50 +00:00
|
|
|
Self { init, super_init }
|
2020-01-05 07:01:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 13:44:50 +00:00
|
|
|
/// Constructs a new initializer from base class' initializer.
|
|
|
|
///
|
2021-03-20 07:45:56 +00:00
|
|
|
/// # Examples
|
2020-01-08 13:44:50 +00:00
|
|
|
/// ```
|
|
|
|
/// # use pyo3::prelude::*;
|
|
|
|
/// #[pyclass]
|
|
|
|
/// struct BaseClass {
|
|
|
|
/// value: u32,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl BaseClass {
|
|
|
|
/// fn new(value: i32) -> PyResult<Self> {
|
|
|
|
/// Ok(Self {
|
|
|
|
/// value: std::convert::TryFrom::try_from(value)?,
|
|
|
|
/// })
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// #[pyclass(extends=BaseClass)]
|
|
|
|
/// struct SubClass {}
|
|
|
|
///
|
|
|
|
/// #[pymethods]
|
|
|
|
/// impl SubClass {
|
|
|
|
/// #[new]
|
|
|
|
/// fn new(value: i32) -> PyResult<PyClassInitializer<Self>> {
|
|
|
|
/// let base_init = PyClassInitializer::from(BaseClass::new(value)?);
|
|
|
|
/// Ok(base_init.add_subclass(SubClass {}))
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-01-05 07:01:05 +00:00
|
|
|
pub fn add_subclass<S>(self, subclass_value: S) -> PyClassInitializer<S>
|
|
|
|
where
|
2021-05-04 20:10:03 +00:00
|
|
|
S: PyClass<BaseType = T>,
|
|
|
|
S::BaseType: PyClassBaseType<Initializer = Self>,
|
2020-01-05 07:01:05 +00:00
|
|
|
{
|
|
|
|
PyClassInitializer::new(subclass_value, self)
|
|
|
|
}
|
|
|
|
|
2020-06-22 14:49:33 +00:00
|
|
|
/// Create a new PyCell and initialize it.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn create_cell(self, py: Python) -> PyResult<*mut PyCell<T>>
|
2020-01-05 07:01:05 +00:00
|
|
|
where
|
|
|
|
T: PyClass,
|
|
|
|
{
|
2020-06-22 20:34:42 +00:00
|
|
|
unsafe { self.create_cell_from_subtype(py, T::type_object_raw(py)) }
|
2020-06-21 14:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new PyCell and initialize it given a typeobject `subtype`.
|
|
|
|
/// Called by our `tp_new` generated by the `#[new]` attribute.
|
2020-06-21 17:38:08 +00:00
|
|
|
///
|
|
|
|
/// # Safety
|
2020-06-22 14:49:33 +00:00
|
|
|
/// `subtype` must be a subclass of T.
|
|
|
|
#[doc(hidden)]
|
2020-06-21 14:38:26 +00:00
|
|
|
pub unsafe fn create_cell_from_subtype(
|
|
|
|
self,
|
|
|
|
py: Python,
|
|
|
|
subtype: *mut crate::ffi::PyTypeObject,
|
|
|
|
) -> PyResult<*mut PyCell<T>>
|
|
|
|
where
|
|
|
|
T: PyClass,
|
|
|
|
{
|
|
|
|
let cell = PyCell::internal_new(py, subtype)?;
|
2020-02-03 13:25:16 +00:00
|
|
|
self.init_class(&mut *cell);
|
|
|
|
Ok(cell)
|
2020-01-05 07:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T> {
|
2020-02-22 15:56:58 +00:00
|
|
|
fn init_class<L: PyLayout<T>>(self, layout: &mut L) {
|
2020-01-05 07:01:05 +00:00
|
|
|
let Self { init, super_init } = self;
|
2021-05-04 20:10:03 +00:00
|
|
|
// Safety: A valid PyLayout must contain the base layout as the first entry, so casting L to
|
|
|
|
// T::BaseType::LayoutAsBase is ok.
|
|
|
|
super_init.init_class(unsafe {
|
|
|
|
&mut *(layout as *mut _ as *mut <T::BaseType as PyClassBaseType>::LayoutAsBase)
|
|
|
|
});
|
2021-01-26 09:52:26 +00:00
|
|
|
layout.py_init(init);
|
2020-01-05 07:01:05 +00:00
|
|
|
}
|
2020-01-08 13:44:50 +00:00
|
|
|
private_impl! {}
|
2020-01-05 07:01:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 13:14:41 +00:00
|
|
|
impl<T> From<T> for PyClassInitializer<T>
|
2020-01-05 07:01:05 +00:00
|
|
|
where
|
|
|
|
T: PyClass,
|
2021-05-04 20:10:03 +00:00
|
|
|
T::BaseType: PyClassBaseType<Initializer = PyNativeTypeInitializer<T::BaseType>>,
|
2020-01-05 07:01:05 +00:00
|
|
|
{
|
2020-01-06 13:14:41 +00:00
|
|
|
fn from(value: T) -> PyClassInitializer<T> {
|
2020-01-07 03:49:36 +00:00
|
|
|
Self::new(value, PyNativeTypeInitializer(PhantomData))
|
2020-01-05 07:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 04:07:14 +00:00
|
|
|
impl<S, B> From<(S, B)> for PyClassInitializer<S>
|
2020-01-05 07:01:05 +00:00
|
|
|
where
|
2021-05-04 20:10:03 +00:00
|
|
|
S: PyClass<BaseType = B>,
|
|
|
|
B: PyClass,
|
|
|
|
B::BaseType: PyClassBaseType<Initializer = PyNativeTypeInitializer<B::BaseType>>,
|
2020-01-05 07:01:05 +00:00
|
|
|
{
|
2020-01-07 04:07:14 +00:00
|
|
|
fn from(sub_and_base: (S, B)) -> PyClassInitializer<S> {
|
|
|
|
let (sub, base) = sub_and_base;
|
|
|
|
PyClassInitializer::from(base).add_subclass(sub)
|
2020-01-05 07:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 16:16:44 +00:00
|
|
|
|
2020-12-13 22:51:19 +00:00
|
|
|
// Implementation used by proc macros to allow anything convertible to PyClassInitializer<T> to be
|
|
|
|
// the return value of pyclass #[new] method (optionally wrapped in `Result<U, E>`).
|
|
|
|
impl<T, U> IntoPyCallbackOutput<PyClassInitializer<T>> for U
|
2020-06-22 16:16:44 +00:00
|
|
|
where
|
|
|
|
T: PyClass,
|
|
|
|
U: Into<PyClassInitializer<T>>,
|
|
|
|
{
|
2020-12-13 22:51:19 +00:00
|
|
|
fn convert(self, _py: Python) -> PyResult<PyClassInitializer<T>> {
|
|
|
|
Ok(self.into())
|
2020-06-22 16:16:44 +00:00
|
|
|
}
|
|
|
|
}
|