[][src]Struct pyo3::pyclass_init::PyClassInitializer

pub struct PyClassInitializer<T: PyClass> { /* fields omitted */ }

Initializer for our #[pyclass] system.

You can use this type to initalize complicatedly nested #[pyclass].

Example

#[pyclass]
struct BaseClass {
    #[pyo3(get)]
    basename: &'static str,
}
#[pyclass(extends=BaseClass)]
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" })
    }
}
let gil = Python::acquire_gil();
let py = gil.python();
let typeobj = py.get_type::<SubSubClass>();
let inst = typeobj.call((), None).unwrap();
py_run!(py, inst, r#"
        assert inst.basename == 'base'
        assert inst.subname == 'sub'
        assert inst.subsubname == 'subsub'"#);

Implementations

impl<T: PyClass> PyClassInitializer<T>[src]

pub fn new(
    init: T,
    super_init: <T::BaseType as PyTypeInfo>::Initializer
) -> Self
[src]

Constract new initialzer from value T and base class' initializer.

We recommend to mainly use add_subclass, instead of directly call new.

pub fn add_subclass<S>(self, subclass_value: S) -> PyClassInitializer<S> where
    S: PyClass + PyTypeInfo<BaseType = T>,
    S::BaseLayout: PySizedLayout<T>,
    S::BaseType: PyTypeInfo<Initializer = Self>, 
[src]

Constructs a new initializer from base class' initializer.

Example

#[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 {}))
    }
}

Trait Implementations

impl<S, B> From<(S, B)> for PyClassInitializer<S> where
    S: PyClass + PyTypeInfo<BaseType = B>,
    S::BaseLayout: PySizedLayout<B>,
    B: PyClass + PyTypeInfo<Initializer = PyClassInitializer<B>>,
    B::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<B::BaseType>>, 
[src]

impl<T> From<T> for PyClassInitializer<T> where
    T: PyClass,
    T::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<T::BaseType>>, 
[src]

impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T>[src]

impl<T, U> TryFrom<Result<U, PyErr>> for PyClassInitializer<T> where
    T: PyClass,
    U: Into<PyClassInitializer<T>>, 
[src]

type Error = PyErr

The type returned in the event of a conversion error.

Auto Trait Implementations

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

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

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

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

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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> FromPy<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> IntoPy<U> for T where
    U: FromPy<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.