Trait FromPyObject
pub trait FromPyObject<'py>: Sized {
// Required method
fn extract_bound(ob: &Bound<'py, PyAny>) -> Result<Self, PyErr> ⓘ;
}
dep_pyo3
and std
only.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(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§
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§
§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
.
§impl<'py> FromPyObject<'py> for f32
impl<'py> FromPyObject<'py> for f32
§impl<'py> FromPyObject<'py> for f64
impl<'py> FromPyObject<'py> for f64
§impl<'py> FromPyObject<'py> for i16
impl<'py> FromPyObject<'py> for i16
§impl<'py> FromPyObject<'py> for i32
impl<'py> FromPyObject<'py> for i32
§impl<'py> FromPyObject<'py> for i64
impl<'py> FromPyObject<'py> for i64
§impl<'py> FromPyObject<'py> for isize
impl<'py> FromPyObject<'py> for isize
§impl<'py> FromPyObject<'py> for u16
impl<'py> FromPyObject<'py> for u16
§impl<'py> FromPyObject<'py> for u32
impl<'py> FromPyObject<'py> for u32
§impl<'py, T0> FromPyObject<'py> for (T0,)where
T0: FromPyObject<'py>,
impl<'py, T0> FromPyObject<'py> for (T0,)where
T0: FromPyObject<'py>,
§impl<'py, T0, T1> FromPyObject<'py> for (T0, T1)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
impl<'py, T0, T1> FromPyObject<'py> for (T0, T1)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
§impl<'py, T0, T1, T2> FromPyObject<'py> for (T0, T1, T2)
impl<'py, T0, T1, T2> FromPyObject<'py> for (T0, T1, T2)
fn extract_bound(obj: &Bound<'py, PyAny>) -> Result<(T0, T1, T2), PyErr> ⓘ
§impl<'py, T0, T1, T2, T3> FromPyObject<'py> for (T0, T1, T2, T3)
impl<'py, T0, T1, T2, T3> FromPyObject<'py> for (T0, T1, T2, T3)
fn extract_bound(obj: &Bound<'py, PyAny>) -> Result<(T0, T1, T2, T3), PyErr> ⓘ
§impl<'py, T0, T1, T2, T3, T4> FromPyObject<'py> for (T0, T1, T2, T3, T4)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
impl<'py, T0, T1, T2, T3, T4> FromPyObject<'py> for (T0, T1, T2, T3, T4)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
fn extract_bound(obj: &Bound<'py, PyAny>) -> Result<(T0, T1, T2, T3, T4), PyErr> ⓘ
§impl<'py, T0, T1, T2, T3, T4, T5> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
impl<'py, T0, T1, T2, T3, T4, T5> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
fn extract_bound( obj: &Bound<'py, PyAny>, ) -> Result<(T0, T1, T2, T3, T4, T5), PyErr> ⓘ
§impl<'py, T0, T1, T2, T3, T4, T5, T6> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
impl<'py, T0, T1, T2, T3, T4, T5, T6> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
fn extract_bound( obj: &Bound<'py, PyAny>, ) -> Result<(T0, T1, T2, T3, T4, T5, T6), PyErr> ⓘ
§impl<'py, T0, T1, T2, T3, T4, T5, T6, T7> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
impl<'py, T0, T1, T2, T3, T4, T5, T6, T7> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
fn extract_bound( obj: &Bound<'py, PyAny>, ) -> Result<(T0, T1, T2, T3, T4, T5, T6, T7), PyErr> ⓘ
§impl<'py, T0, T1, T2, T3, T4, T5, T6, T7, T8> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
T8: FromPyObject<'py>,
impl<'py, T0, T1, T2, T3, T4, T5, T6, T7, T8> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
T8: FromPyObject<'py>,
fn extract_bound( obj: &Bound<'py, PyAny>, ) -> Result<(T0, T1, T2, T3, T4, T5, T6, T7, T8), PyErr> ⓘ
§impl<'py, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
T8: FromPyObject<'py>,
T9: FromPyObject<'py>,
impl<'py, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
T8: FromPyObject<'py>,
T9: FromPyObject<'py>,
fn extract_bound( obj: &Bound<'py, PyAny>, ) -> Result<(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9), PyErr> ⓘ
§impl<'py, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
T8: FromPyObject<'py>,
T9: FromPyObject<'py>,
T10: FromPyObject<'py>,
impl<'py, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
T8: FromPyObject<'py>,
T9: FromPyObject<'py>,
T10: FromPyObject<'py>,
fn extract_bound( obj: &Bound<'py, PyAny>, ) -> Result<(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10), PyErr> ⓘ
§impl<'py, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
T8: FromPyObject<'py>,
T9: FromPyObject<'py>,
T10: FromPyObject<'py>,
T11: FromPyObject<'py>,
impl<'py, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)where
T0: FromPyObject<'py>,
T1: FromPyObject<'py>,
T2: FromPyObject<'py>,
T3: FromPyObject<'py>,
T4: FromPyObject<'py>,
T5: FromPyObject<'py>,
T6: FromPyObject<'py>,
T7: FromPyObject<'py>,
T8: FromPyObject<'py>,
T9: FromPyObject<'py>,
T10: FromPyObject<'py>,
T11: FromPyObject<'py>,
fn extract_bound( obj: &Bound<'py, PyAny>, ) -> Result<(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11), PyErr> ⓘ
§impl<'py, T, const N: usize> FromPyObject<'py> for [T; N]where
T: FromPyObject<'py>,
impl<'py, T, const N: usize> FromPyObject<'py> for [T; N]where
T: FromPyObject<'py>,
Implementors§
impl FromPyObject<'_> for IpAddr
impl FromPyObject<'_> for NonZero<i8>
impl FromPyObject<'_> for NonZero<i16>
impl FromPyObject<'_> for NonZero<i32>
impl FromPyObject<'_> for NonZero<i64>
impl FromPyObject<'_> for NonZero<i128>
impl FromPyObject<'_> for NonZero<isize>
impl FromPyObject<'_> for NonZero<u8>
impl FromPyObject<'_> for NonZero<u16>
impl FromPyObject<'_> for NonZero<u32>
impl FromPyObject<'_> for NonZero<u64>
impl FromPyObject<'_> for NonZero<u128>
impl FromPyObject<'_> for NonZero<usize>
impl FromPyObject<'_> for PathBuf
impl FromPyObject<'_> for String
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.