pub trait IntoPyPointer {
    // Required method
    fn into_ptr(self) -> *mut PyObject;
}
Expand description

Returns an owned pointer to a Python object.

The returned pointer will be valid until you decrease its reference count. It may be null depending on the implementation. It is your responsibility to decrease the reference count of the pointer to avoid leaking memory.

Examples

use pyo3::prelude::*;
use pyo3::IntoPyPointer;
use pyo3::types::PyString;
use pyo3::ffi;

Python::with_gil(|py| {
    let s: Py<PyString> = "foo".into_py(py);
    let ptr = s.into_ptr();

    let is_really_a_pystring = unsafe { ffi::PyUnicode_CheckExact(ptr) };
    assert_eq!(is_really_a_pystring, 1);

    // Because we took ownership of the pointer,
    // we must manually decrement it to avoid leaking memory
    unsafe { ffi::Py_DECREF(ptr) };
});

Required Methods§

source

fn into_ptr(self) -> *mut PyObject

Returns the underlying FFI pointer as an owned pointer.

If self has ownership of the underlying pointer, it will “steal” ownership of it.

Implementations on Foreign Types§

source§

impl<T> IntoPyPointer for Option<T>where
T: IntoPyPointer,

Convert None into a null pointer.

source§

impl<'a, T> IntoPyPointer for &'a Twhere
T: AsPyPointer,

Implementors§

source§

impl<T> IntoPyPointer for Py<T>