[−][src]Struct pyo3::ManagedPyRef
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]
Auto Trait Implementations
impl<'p, T: ?Sized> Unpin for ManagedPyRef<'p, T> where
T: Unpin,
T: Unpin,
impl<'p, T> !Sync for ManagedPyRef<'p, T>
impl<'p, T> !Send for ManagedPyRef<'p, T>
impl<'p, T> !RefUnwindSafe for ManagedPyRef<'p, T>
impl<'p, T> !UnwindSafe for ManagedPyRef<'p, T>
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,