[][src]Struct pyo3::pycell::PyRef

pub struct PyRef<'p, T: PyClass> { /* fields omitted */ }

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

#[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]

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]

impl<T: PyClass + Debug, '_> Debug for PyRef<'_, T>[src]

impl<'p, T: PyClass> Deref for PyRef<'p, T>[src]

type Target = T

The resulting type after dereferencing.

impl<'p, T: PyClass> Drop for PyRef<'p, T>[src]

impl<'a, T> From<PyRef<'a, T>> for Py<T> where
    T: PyClass
[src]

impl<'p, T: PyClass> FromPy<PyRef<'p, T>> for PyObject[src]

impl<'a, T> FromPyObject<'a> for PyRef<'a, T> where
    T: PyClass
[src]

impl<'a, T: PyClass> TryFrom<&'a PyCell<T>> for PyRef<'a, T>[src]

type Error = PyBorrowError

The type returned in the event of a conversion error.

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]

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<'a, T> FromPyObject<'a> for T where
    T: PyClass + Clone
[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.