[][src]Trait pyo3::AsPyRef

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

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

Examples

PyObject::as_ref returns &PyAny.

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);

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

#[pyclass]
struct Counter {
    count: usize,
}
let counter = {
    let gil = Python::acquire_gil();
    let py = gil.python();
    Py::new(py, Counter { count: 0}).unwrap()
};
let gil = Python::acquire_gil();
let py = gil.python();
let counter_cell: &PyCell<Counter> = counter.as_ref(py);
let counter_ref = counter_cell.borrow();
assert_eq!(counter_ref.count, 0);

Associated Types

type Target

Loading content...

Required methods

fn as_ref<'p>(&'p self, py: Python<'p>) -> &'p 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...