pub struct PyClassInitializer<T>where
    T: PyClass,{ /* private fields */ }
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'"#
    );
});

Implementations§

source§

impl<T> PyClassInitializer<T>where T: PyClass,

source

pub fn new( init: T, super_init: <<T as PyClassImpl>::BaseType as PyClassBaseType>::Initializer ) -> PyClassInitializer<T>

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.

source

pub fn add_subclass<S>(self, subclass_value: S) -> PyClassInitializer<S>where S: PyClass<BaseType = T>, <S as PyClassImpl>::BaseType: PyClassBaseType<Initializer = PyClassInitializer<T>>,

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(())
    })
}

Trait Implementations§

source§

impl<S, B> From<(S, B)> for PyClassInitializer<S>where S: PyClass<BaseType = B>, B: PyClass, <B as PyClassImpl>::BaseType: PyClassBaseType<Initializer = PyNativeTypeInitializer<<B as PyClassImpl>::BaseType>>,

source§

fn from(sub_and_base: (S, B)) -> PyClassInitializer<S>

Converts to this type from the input type.
source§

impl<T> From<T> for PyClassInitializer<T>where T: PyClass, <T as PyClassImpl>::BaseType: PyClassBaseType<Initializer = PyNativeTypeInitializer<<T as PyClassImpl>::BaseType>>,

source§

fn from(value: T) -> PyClassInitializer<T>

Converts to this type from the input type.
source§

impl<T> PyObjectInit<T> for PyClassInitializer<T>where T: PyClass,

source§

unsafe fn into_new_object( self, py: Python<'_>, subtype: *mut PyTypeObject ) -> Result<*mut PyObject, PyErr>

Safety Read more
source§

fn __private__(&self) -> PrivateMarker

This trait is private to implement; this method exists to make it impossible to implement outside the crate.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for PyClassInitializer<T>where T: RefUnwindSafe, <<T as PyClassImpl>::BaseType as PyClassBaseType>::Initializer: RefUnwindSafe,

§

impl<T> Send for PyClassInitializer<T>where T: Send, <<T as PyClassImpl>::BaseType as PyClassBaseType>::Initializer: Send,

§

impl<T> Sync for PyClassInitializer<T>where T: Sync, <<T as PyClassImpl>::BaseType as PyClassBaseType>::Initializer: Sync,

§

impl<T> Unpin for PyClassInitializer<T>where T: Unpin, <<T as PyClassImpl>::BaseType as PyClassBaseType>::Initializer: Unpin,

§

impl<T> UnwindSafe for PyClassInitializer<T>where T: UnwindSafe, <<T as PyClassImpl>::BaseType as PyClassBaseType>::Initializer: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> Ungil for Twhere T: Send,