# pyo3::pycell::PyRef - Rust
Wraps a borrowed reference to a value in a `PyCell<T>`.
Keywords: rust, rustlang, rust-lang, PyRef

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

    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

- You need to access the pointer of `PyCell`.
- You want to get super class.

    #[pyclass(subclass)]
    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(subclass)]
    struct Base1 {
        name1: &'static str,
    }
    #[pyclass(extends=Base1, subclass)]
    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]

#### `fn as_ptr(&self) -> *mut PyObject` [src]

Retrieves the underlying FFI pointer (as a borrowed pointer).

### `impl<'p, T, U> AsRef<U> for PyRef<'p, T> where
T: PyClass + PyTypeInfo<BaseType = U, BaseLayout = PyCellInner<U>>,
U: PyClass,` [src]

#### `fn as_ref(&self) -> &T::BaseType` [src]

Performs the conversion.

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter. Read more

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

#### `type Target = T`

The resulting type after dereferencing.

#### `fn deref(&self) -> &T` [src]

Dereferences the value.

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

#### `fn drop(&mut self)` [src]

Executes the destructor for this type. Read more

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

#### `fn from(pyref: PyRef<'a, T>) -> Self` [src]

Performs the conversion.

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

#### `fn extract(obj: &'a PyAny) -> PyResult<Self>` [src]

Extracts `Self` from the source `PyObject`.

### `impl<T: PyClass> IntoPy<Py<PyAny>> for PyRef<'_, T>` [src]

#### `fn into_py(self, py: Python<'_>) -> PyObject` [src]

Performs the conversion.

### `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.

#### `fn try_from(cell: &'a PyCell<T>) -> Result<Self, Self::Error>` [src]

Performs the conversion.

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

#### `pub fn type_id(&self) -> TypeId` [src]

Gets the `TypeId` of `self`. Read more

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

#### `pub fn borrow(&self) -> &T` [src]

Immutably borrows from an owned value. Read more

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

#### `pub fn borrow_mut(&mut self) -> &mut T` [src]

Mutably borrows from an owned value. Read more

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

#### `pub fn from(t: T) -> T` [src]

Performs the conversion.

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

#### `pub fn into(self) -> U` [src]

Performs the conversion.

### `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.

#### `pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>` [src]

Performs the conversion.

### `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.

#### `pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>` [src]

Performs the conversion.