[−][src]Struct pyo3::pyclass_init::PyClassInitializer
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]
init: T,
super_init: <T::BaseType as PyTypeInfo>::Initializer
) -> Self
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]
S: PyClass + PyTypeInfo<BaseType = T>,
S::BaseLayout: PySizedLayout<T>,
S::BaseType: PyTypeInfo<Initializer = Self>,
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]
S: PyClass + PyTypeInfo<BaseType = B>,
S::BaseLayout: PySizedLayout<B>,
B: PyClass + PyTypeInfo<Initializer = PyClassInitializer<B>>,
B::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<B::BaseType>>,
fn from(sub_and_base: (S, B)) -> PyClassInitializer<S>
[src]
impl<T> From<T> for PyClassInitializer<T> where
T: PyClass,
T::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<T::BaseType>>,
[src]
T: PyClass,
T::BaseType: PyTypeInfo<Initializer = PyNativeTypeInitializer<T::BaseType>>,
fn from(value: T) -> PyClassInitializer<T>
[src]
impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T>
[src]
fn init_class<L: PyLayout<T>>(self, layout: &mut L)
[src]
fn __private__(&self) -> PrivateMarker
[src]
impl<T, U> TryFrom<Result<U, PyErr>> for PyClassInitializer<T> where
T: PyClass,
U: Into<PyClassInitializer<T>>,
[src]
T: PyClass,
U: Into<PyClassInitializer<T>>,
Auto Trait Implementations
impl<T> RefUnwindSafe for PyClassInitializer<T> where
T: RefUnwindSafe,
<<T as PyTypeInfo>::BaseType as PyTypeInfo>::Initializer: RefUnwindSafe,
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,
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,
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,
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,
T: UnwindSafe,
<<T as PyTypeInfo>::BaseType as PyTypeInfo>::Initializer: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[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]
U: From<T>,
impl<T, U> IntoPy<U> for T where
U: FromPy<T>,
[src]
U: FromPy<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,