[][src]Trait pyo3::AsPyRef

pub trait AsPyRef: Sized {
    type Target;
    fn as_ref(&self, py: Python) -> &Self::Target;
}

Retrieves &'py types from Py<T> or PyObject.

Examples

PyObject::as_ref returns &PyAny.

use pyo3::ObjectProtocol;
let obj: PyObject = {
    let gil = Python::acquire_gil();
    let py = gil.python();
    py.eval("[]", None, None).unwrap().to_object(py)
};
let gil = Python::acquire_gil();
let py = gil.python();
assert_eq!(obj.as_ref(py).len().unwrap(), 0);  // PyAny implements ObjectProtocol

Py<T>::as_ref returns &PyDict, &PyList or so for native types, and &PyCell<T> for #[pyclass].

use pyo3::ObjectProtocol;
let obj: PyObject = {
    let gil = Python::acquire_gil();
    let py = gil.python();
    py.eval("[]", None, None).unwrap().to_object(py)
};
let gil = Python::acquire_gil();
let py = gil.python();
assert_eq!(obj.as_ref(py).len().unwrap(), 0);  // PyAny implements ObjectProtocol

Associated Types

type Target

Loading content...

Required methods

fn as_ref(&self, py: Python) -> &Self::Target

Return reference to object.

Loading content...

Implementors

impl AsPyRef for PyObject[src]

type Target = PyAny

impl<T> AsPyRef for Py<T> where
    T: PyTypeInfo
[src]

type Target = T::AsRefTarget

Loading content...