[−][src]Struct pyo3::PyRef
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 pyclass
es, because they're not Python
objects until copied to the Python heap. So, instead of treating &pyclass
es 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<'a, T> PyRef<'a, T> where
T: PyTypeInfo + PyTypeObject + PyTypeCreate,
[src]
T: PyTypeInfo + PyTypeObject + PyTypeCreate,
Trait Implementations
impl<'a, T: PyTypeInfo> AsPyPointer for PyRef<'a, T>
[src]
impl<'a, T: PyTypeInfo> ToPyObject for PyRef<'a, T>
[src]
impl<'a, T> From<PyRef<'a, T>> for Py<T> where
T: PyTypeInfo,
[src]
T: PyTypeInfo,
impl<'a, T> From<PyRef<'a, T>> for &'a PyAny where
T: PyTypeInfo,
[src]
T: PyTypeInfo,
impl<'a, T: PyTypeInfo> Deref for PyRef<'a, T>
[src]
impl<'a, T: Debug + PyTypeInfo> Debug for PyRef<'a, T>
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom 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> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,