pub trait FromPyObject<'py>: Sized {
// Required method
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self>;
}
Expand description
Extract a type from a Python object.
Normal usage is through the extract
methods on Bound
and Py
, which forward to this trait.
§Examples
use pyo3::prelude::*;
use pyo3::types::PyString;
Python::with_gil(|py| {
// Calling `.extract()` on a `Bound` smart pointer
let obj: Bound<'_, PyString> = PyString::new_bound(py, "blah");
let s: String = obj.extract()?;
// Calling `.extract(py)` on a `Py` smart pointer
let obj: Py<PyString> = obj.unbind();
let s: String = obj.extract(py)?;
})
During the migration of PyO3 from the “GIL Refs” API to the Bound<T>
smart pointer, this trait
has two methods extract
and extract_bound
which are defaulted to call each other. To avoid
infinite recursion, implementors must implement at least one of these methods. The recommendation
is to implement extract_bound
and leave extract
as the default implementation.
Required Methods§
Sourcefn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self>
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self>
Extracts Self
from the bound smart pointer obj
.
Implementors are encouraged to implement this method and leave extract
defaulted, as
this will be most compatible with PyO3’s future API.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl FromPyObject<'_> for IpAddr
impl FromPyObject<'_> for IpAddr
Source§impl FromPyObject<'_> for bool
Converts a Python bool
to a Rust bool
.
impl FromPyObject<'_> for bool
Converts a Python bool
to a Rust bool
.
Fails with TypeError
if the input is not a Python bool
.
Source§impl FromPyObject<'_> for char
impl FromPyObject<'_> for char
Source§impl FromPyObject<'_> for i128
impl FromPyObject<'_> for i128
Source§impl FromPyObject<'_> for u64
impl FromPyObject<'_> for u64
Source§impl FromPyObject<'_> for u128
impl FromPyObject<'_> for u128
Source§impl FromPyObject<'_> for usize
impl FromPyObject<'_> for usize
Source§impl FromPyObject<'_> for String
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.
impl FromPyObject<'_> for String
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.