[−][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<'p, T> PyRef<'p, 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<'p, T> FromPyPointer<'p> for PyRef<'p, T> where
T: PyTypeInfo,
[src]
T: PyTypeInfo,
unsafe fn from_owned_ptr_or_opt(
py: Python<'p>,
ptr: *mut PyObject
) -> Option<Self>
[src]
py: Python<'p>,
ptr: *mut PyObject
) -> Option<Self>
unsafe fn from_borrowed_ptr_or_opt(
py: Python<'p>,
ptr: *mut PyObject
) -> Option<Self>
[src]
py: Python<'p>,
ptr: *mut PyObject
) -> Option<Self>
unsafe fn from_owned_ptr_or_panic(py: Python<'p>, ptr: *mut PyObject) -> Self
[src]
unsafe fn from_owned_ptr(py: Python<'p>, ptr: *mut PyObject) -> Self
[src]
unsafe fn from_owned_ptr_or_err(
py: Python<'p>,
ptr: *mut PyObject
) -> PyResult<Self>
[src]
py: Python<'p>,
ptr: *mut PyObject
) -> PyResult<Self>
unsafe fn from_borrowed_ptr_or_panic(py: Python<'p>, ptr: *mut PyObject) -> Self
[src]
unsafe fn from_borrowed_ptr(py: Python<'p>, ptr: *mut PyObject) -> Self
[src]
unsafe fn from_borrowed_ptr_or_err(
py: Python<'p>,
ptr: *mut PyObject
) -> PyResult<Self>
[src]
py: Python<'p>,
ptr: *mut PyObject
) -> PyResult<Self>
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
impl<'a, T> Unpin for PyRef<'a, T>
impl<'a, T> !Sync for PyRef<'a, T>
impl<'a, T> !Send for PyRef<'a, T>
impl<'a, T> !RefUnwindSafe for PyRef<'a, T>
impl<'a, T> UnwindSafe for PyRef<'a, T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<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>,
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> 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> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,