[][src]Struct pyo3::PyRef

pub struct PyRef<'a, T: PyTypeInfo>(_, _);

A special reference of type T. PyRef<T> refers a instance of T, which exists in the Python heap as a part of a Python object.

We can't implement AsPyPointer or ToPyObject for pyclasses, because they're not Python objects until copied to the Python heap. So, instead of treating &pyclasses as Python objects, we need to use special reference types PyRef and PyRefMut.

Example

use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
#[pyclass]
struct Point {
    x: i32,
    y: i32,
}
#[pymethods]
impl Point {
    fn length(&self) -> i32 {
        self.x * self.y
    }
}
let gil = Python::acquire_gil();
let py = gil.python();
let obj = PyRef::new(gil.python(), Point { x: 3, y: 4 }).unwrap();
let d = [("p", obj)].into_py_dict(py);
py.run("assert p.length() == 12", None, Some(d)).unwrap();

Methods

impl<'p, T> PyRef<'p, T> where
    T: PyTypeInfo + PyTypeObject + PyTypeCreate
[src]

pub fn new(py: Python<'p>, value: T) -> PyResult<PyRef<T>>[src]

Trait Implementations

impl<'a, T: PyTypeInfo> AsPyPointer for PyRef<'a, T>[src]

impl<'a, T: Debug + PyTypeInfo> Debug for PyRef<'a, T>[src]

impl<'a, T: PyTypeInfo> Deref for PyRef<'a, T>[src]

type Target = T

The resulting type after dereferencing.

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

impl<'a, T> From<PyRef<'a, T>> for &'a PyAny where
    T: PyTypeInfo
[src]

impl<'p, T> FromPyPointer<'p> for PyRef<'p, T> where
    T: PyTypeInfo
[src]

impl<'a, T: PyTypeInfo> IntoPy<PyObject> for PyRef<'a, T>[src]

impl<'a, T: PyTypeInfo> ToPyObject for PyRef<'a, T>[src]

Auto Trait Implementations

impl<'a, T> !RefUnwindSafe for PyRef<'a, T>

impl<'a, T> !Send for PyRef<'a, T>

impl<'a, T> !Sync for PyRef<'a, T>

impl<'a, T> Unpin for PyRef<'a, T>

impl<'a, T> UnwindSafe for PyRef<'a, T> where
    T: RefUnwindSafe

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, U> Into<U> for T where
    U: From<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.