pub struct PyClassInitializer<T: PyClass>(PyClassInitializerImpl<T>);Expand description
Initializer for our #[pyclass] system.
You can use this type to initialize complicatedly nested #[pyclass].
§Examples
#[pyclass(subclass)]
struct BaseClass {
    #[pyo3(get)]
    basename: &'static str,
}
#[pyclass(extends=BaseClass, subclass)]
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> {
        PyClassInitializer::from(BaseClass { basename: "base" })
            .add_subclass(SubClass { subname: "sub" })
            .add_subclass(SubSubClass {
                subsubname: "subsub",
            })
    }
}
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'"#
    );
});Tuple Fields§
§0: PyClassInitializerImpl<T>Implementations§
Source§impl<T: PyClass> PyClassInitializer<T>
 
impl<T: PyClass> PyClassInitializer<T>
Sourcepub fn new(
    init: T,
    super_init: <T::BaseType as PyClassBaseType>::Initializer,
) -> Self
 
pub fn new( init: T, super_init: <T::BaseType as PyClassBaseType>::Initializer, ) -> Self
Constructs a new initializer from value T and base class’ initializer.
It is recommended to use add_subclass instead of this method for most usage.
Sourcepub fn add_subclass<S>(self, subclass_value: S) -> PyClassInitializer<S>
 
pub fn add_subclass<S>(self, subclass_value: S) -> PyClassInitializer<S>
Constructs a new initializer from an initializer for the base class.
§Examples
use pyo3::prelude::*;
#[pyclass(subclass)]
struct BaseClass {
    #[pyo3(get)]
    value: i32,
}
impl BaseClass {
    fn new(value: i32) -> PyResult<Self> {
        Ok(Self { 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 {}))
    }
}
fn main() -> PyResult<()> {
    Python::with_gil(|py| {
        let m = PyModule::new(py, "example")?;
        m.add_class::<SubClass>()?;
        m.add_class::<BaseClass>()?;
        let instance = m.getattr("SubClass")?.call1((92,))?;
        // `SubClass` does not have a `value` attribute, but `BaseClass` does.
        let n = instance.getattr("value")?.extract::<i32>()?;
        assert_eq!(n, 92);
        Ok(())
    })
}Sourcepub(crate) fn create_class_object(
    self,
    py: Python<'_>,
) -> PyResult<Bound<'_, T>>where
    T: PyClass,
 
pub(crate) fn create_class_object(
    self,
    py: Python<'_>,
) -> PyResult<Bound<'_, T>>where
    T: PyClass,
Creates a new PyCell and initializes it.
Sourcepub(crate) unsafe fn create_class_object_of_type(
    self,
    py: Python<'_>,
    target_type: *mut PyTypeObject,
) -> PyResult<Bound<'_, T>>where
    T: PyClass,
 
pub(crate) unsafe fn create_class_object_of_type(
    self,
    py: Python<'_>,
    target_type: *mut PyTypeObject,
) -> PyResult<Bound<'_, T>>where
    T: PyClass,
Creates a new class object and initializes it given a typeobject subtype.
§Safety
subtype must be a valid pointer to the type object of T or a subclass.
Trait Implementations§
Source§impl<S, B> From<(S, B)> for PyClassInitializer<S>where
    S: PyClass<BaseType = B>,
    B: PyClass + PyClassBaseType<Initializer = PyClassInitializer<B>>,
    B::BaseType: PyClassBaseType<Initializer = PyNativeTypeInitializer<B::BaseType>>,
 
impl<S, B> From<(S, B)> for PyClassInitializer<S>where
    S: PyClass<BaseType = B>,
    B: PyClass + PyClassBaseType<Initializer = PyClassInitializer<B>>,
    B::BaseType: PyClassBaseType<Initializer = PyNativeTypeInitializer<B::BaseType>>,
Source§fn from(sub_and_base: (S, B)) -> PyClassInitializer<S>
 
fn from(sub_and_base: (S, B)) -> PyClassInitializer<S>
Converts to this type from the input type.
Source§impl<'py, T: PyClass> From<Bound<'py, T>> for PyClassInitializer<T>
 
impl<'py, T: PyClass> From<Bound<'py, T>> for PyClassInitializer<T>
Source§fn from(value: Bound<'py, T>) -> PyClassInitializer<T>
 
fn from(value: Bound<'py, T>) -> PyClassInitializer<T>
Converts to this type from the input type.
Source§impl<T: PyClass> From<Py<T>> for PyClassInitializer<T>
 
impl<T: PyClass> From<Py<T>> for PyClassInitializer<T>
Source§fn from(value: Py<T>) -> PyClassInitializer<T>
 
fn from(value: Py<T>) -> PyClassInitializer<T>
Converts to this type from the input type.
Source§impl<T> From<T> for PyClassInitializer<T>
 
impl<T> From<T> for PyClassInitializer<T>
Source§fn from(value: T) -> PyClassInitializer<T>
 
fn from(value: T) -> PyClassInitializer<T>
Converts to this type from the input type.
Source§impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T>
 
impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T>
Source§unsafe fn into_new_object(
    self,
    py: Python<'_>,
    subtype: *mut PyTypeObject,
) -> PyResult<*mut PyObject>
 
unsafe fn into_new_object( self, py: Python<'_>, subtype: *mut PyTypeObject, ) -> PyResult<*mut PyObject>
Safety Read more
fn can_be_subclassed(&self) -> bool
impl<T: PyClass> Sealed for PyClassInitializer<T>
Auto Trait Implementations§
impl<T> Freeze for PyClassInitializer<T>
impl<T> RefUnwindSafe for PyClassInitializer<T>where
    T: RefUnwindSafe,
    <<T as PyClassImpl>::BaseType as PyClassBaseType>::Initializer: RefUnwindSafe,
impl<T> Send for PyClassInitializer<T>
impl<T> Sync for PyClassInitializer<T>
impl<T> Unpin for PyClassInitializer<T>
impl<T> UnwindSafe for PyClassInitializer<T>
Blanket Implementations§
Source§impl<T> AssertNotZeroSized for T
 
impl<T> AssertNotZeroSized for T
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
 
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
 
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts 
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
 
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts 
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T, U> IntoPyCallbackOutput<'_, PyClassInitializer<T>> for U
 
impl<T, U> IntoPyCallbackOutput<'_, PyClassInitializer<T>> for U
Source§impl<T> SizedTypeProperties for T
 
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)] const IS_ZST: bool = _
 
#[doc(hidden)] const IS_ZST: bool = _
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)] const LAYOUT: Layout = _
 
#[doc(hidden)] const LAYOUT: Layout = _
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)] const MAX_SLICE_LEN: usize = _
 
#[doc(hidden)] const MAX_SLICE_LEN: usize = _
🔬This is a nightly-only experimental API. (
sized_type_properties)The largest safe length for a 
[Self]. Read more