[−][src]Struct pyo3::pycell::PyRef
Wraps a borrowed reference to a value in a PyCell<T>
.
See the PyCell
documentation for more.
Example
You can use PyRef
as an alternative of &self
receiver when
- You need to access the pointer of
PyCell
. - You want to get super class.
#[pyclass] struct Parent { basename: &'static str, } #[pyclass(extends=Parent)] struct Child { name: &'static str, } #[pymethods] impl Child { #[new] fn new() -> (Self, Parent) { (Child { name: "Caterpillar" }, Parent { basename: "Butterfly" }) } fn format(slf: PyRef<Self>) -> String { // We can get *mut ffi::PyObject from PyRef use pyo3::AsPyPointer; let refcnt = unsafe { pyo3::ffi::Py_REFCNT(slf.as_ptr()) }; // We can get &Self::BaseType by as_ref let basename = slf.as_ref().basename; format!("{}(base: {}, cnt: {})", slf.name, basename, refcnt) } }
Implementations
impl<'p, T: PyClass> PyRef<'p, T>
[src]
pub fn py(&self) -> Python<'_>
[src]
Returns Python
token.
This function is safe since PyRef has the same lifetime as a GILGuard
.
impl<'p, T, U> PyRef<'p, T> where
T: PyClass + PyTypeInfo<BaseType = U, BaseLayout = PyCellInner<U>>,
U: PyClass,
[src]
T: PyClass + PyTypeInfo<BaseType = U, BaseLayout = PyCellInner<U>>,
U: PyClass,
pub fn into_super(self) -> PyRef<'p, U>
[src]
Get PyRef<T::BaseType>
.
You can use this method to get super class of super class.
Examples
#[pyclass] struct Base1 { name1: &'static str, } #[pyclass(extends=Base1)] struct Base2 { name2: &'static str, } #[pyclass(extends=Base2)] struct Sub { name3: &'static str, } #[pymethods] impl Sub { #[new] fn new() -> PyClassInitializer<Self> { PyClassInitializer::from(Base1{ name1: "base1" }) .add_subclass(Base2 { name2: "base2" }) .add_subclass(Self { name3: "sub" }) } fn name(slf: PyRef<Self>) -> String { let subname = slf.name3; let super_ = slf.into_super(); format!("{} {} {}", super_.as_ref().name1, super_.name2, subname) } }
Trait Implementations
impl<'a, T: PyClass> AsPyPointer for PyRef<'a, T>
[src]
impl<'p, T, U> AsRef<U> for PyRef<'p, T> where
T: PyClass + PyTypeInfo<BaseType = U, BaseLayout = PyCellInner<U>>,
U: PyClass,
[src]
T: PyClass + PyTypeInfo<BaseType = U, BaseLayout = PyCellInner<U>>,
U: PyClass,
impl<T: PyClass + Debug, '_> Debug for PyRef<'_, T>
[src]
impl<'p, T: PyClass> Deref for PyRef<'p, T>
[src]
impl<'p, T: PyClass> Drop for PyRef<'p, T>
[src]
impl<'a, T> From<PyRef<'a, T>> for Py<T> where
T: PyClass,
[src]
T: PyClass,
impl<'p, T: PyClass> FromPy<PyRef<'p, T>> for PyObject
[src]
impl<'a, T> FromPyObject<'a> for PyRef<'a, T> where
T: PyClass,
[src]
T: PyClass,
impl<'a, T: PyClass> TryFrom<&'a PyCell<T>> for PyRef<'a, T>
[src]
Auto Trait Implementations
impl<'p, T> !RefUnwindSafe for PyRef<'p, T>
impl<'p, T> !Send for PyRef<'p, T>
impl<'p, T> !Sync for PyRef<'p, T>
impl<'p, T> Unpin for PyRef<'p, T>
impl<'p, T> !UnwindSafe for PyRef<'p, T>
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<'a, T> FromPyObject<'a> for T where
T: PyClass + Clone,
[src]
T: PyClass + Clone,
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>,