[][src]Struct pyo3::ManagedPyRef

#[repr(transparent)]
pub struct ManagedPyRef<'p, T: ToPyObject + ?Sized> { /* fields omitted */ }

Reference to a converted ToPyObject.

Many methods want to take anything that can be converted into a python object. This type takes care of both types types that are already python object (i.e. implement AsPyPointer) and those that don't (i.e. ToPyObject types). For the AsPyPointer types, we just use the borrowed pointer, which is a lot faster and simpler than creating a new extra object. The remaning ToPyObject types are converted to python objects, the owned pointer is stored and decref'd on drop.

Example

use pyo3::ffi;
use pyo3::{ToPyObject, AsPyPointer, PyNativeType, ManagedPyRef};
use pyo3::types::{PyDict, PyAny};

pub fn get_dict_item<'p>(dict: &'p PyDict, key: &impl ToPyObject) -> Option<&'p PyAny> {
    let key = ManagedPyRef::from_to_pyobject(dict.py(), key);
    unsafe {
        dict.py().from_borrowed_ptr_or_opt(ffi::PyDict_GetItem(dict.as_ptr(), key.as_ptr()))
    }
}

Methods

impl<'p, T: ToPyObject> ManagedPyRef<'p, T>[src]

This should eventually be replaced with a generic IntoPy trait impl by figuring out the correct lifetime annotation to make the compiler happy

pub fn from_to_pyobject(py: Python<'p>, to_pyobject: &T) -> Self[src]

Trait Implementations

impl<'p, T: ToPyObject> AsPyPointer for ManagedPyRef<'p, T>[src]

impl<'p, T: ToPyObject + ?Sized> Drop for ManagedPyRef<'p, T>[src]

fn drop(&mut self)[src]

Uses the internal [ManagedPyRefDispatch] trait to get the right drop impl without causing a specialization error

Auto Trait Implementations

impl<'p, T: ?Sized> Unpin for ManagedPyRef<'p, T> where
    T: Unpin

impl<'p, T> !Sync for ManagedPyRef<'p, T>

impl<'p, T> !Send for ManagedPyRef<'p, T>

impl<'p, T> !UnwindSafe for ManagedPyRef<'p, T>

impl<'p, T> !RefUnwindSafe for ManagedPyRef<'p, T>

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for 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.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]