pyo3::conversion

Trait FromPyObject

Source
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§

Source

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

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for bool

Converts a Python bool to a Rust bool.

Fails with TypeError if the input is not a Python bool.

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for char

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for i128

Source§

impl FromPyObject<'_> for u64

Source§

impl FromPyObject<'_> for u128

Source§

impl FromPyObject<'_> for usize

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for String

Allows extracting strings from Python objects. Accepts Python str and unicode objects.

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for Duration

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for OsString

Source§

fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for PathBuf

Source§

fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for SystemTime

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroI8

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroI16

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroI32

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroI64

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroI128

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroIsize

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroU8

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroU16

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroU32

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroU64

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroU128

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl FromPyObject<'_> for NonZeroUsize

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for f32

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for f64

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for i8

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for i16

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for i32

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for i64

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for isize

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for u8

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for u16

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl<'py> FromPyObject<'py> for u32

Source§

fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>

Source§

impl<'py, K> FromPyObject<'py> for BTreeSet<K>
where K: FromPyObject<'py> + Ord,

Source§

fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, K, S> FromPyObject<'py> for HashSet<K, S>
where K: FromPyObject<'py> + Eq + Hash, S: BuildHasher + Default,

Source§

fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, K, V> FromPyObject<'py> for BTreeMap<K, V>
where K: FromPyObject<'py> + Ord, V: FromPyObject<'py>,

Source§

fn extract_bound(ob: &Bound<'py, PyAny>) -> Result<Self, PyErr>

Source§

impl<'py, K, V, S> FromPyObject<'py> for HashMap<K, V, S>
where K: FromPyObject<'py> + Eq + Hash, V: FromPyObject<'py>, S: BuildHasher + Default,

Source§

fn extract_bound(ob: &Bound<'py, PyAny>) -> Result<Self, PyErr>

Source§

impl<'py, T0: FromPyObject<'py>> FromPyObject<'py> for (T0,)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T0: FromPyObject<'py>, T1: FromPyObject<'py>> FromPyObject<'py> for (T0, T1)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T0: FromPyObject<'py>, T1: FromPyObject<'py>, T2: FromPyObject<'py>> FromPyObject<'py> for (T0, T1, T2)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T0: FromPyObject<'py>, T1: FromPyObject<'py>, T2: FromPyObject<'py>, T3: FromPyObject<'py>> FromPyObject<'py> for (T0, T1, T2, T3)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T0: FromPyObject<'py>, T1: FromPyObject<'py>, T2: FromPyObject<'py>, T3: FromPyObject<'py>, T4: FromPyObject<'py>> FromPyObject<'py> for (T0, T1, T2, T3, T4)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T0: FromPyObject<'py>, T1: FromPyObject<'py>, T2: FromPyObject<'py>, T3: FromPyObject<'py>, T4: FromPyObject<'py>, T5: FromPyObject<'py>> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T0: FromPyObject<'py>, T1: FromPyObject<'py>, T2: FromPyObject<'py>, T3: FromPyObject<'py>, T4: FromPyObject<'py>, T5: FromPyObject<'py>, T6: FromPyObject<'py>> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T0: FromPyObject<'py>, T1: FromPyObject<'py>, T2: FromPyObject<'py>, T3: FromPyObject<'py>, T4: FromPyObject<'py>, T5: FromPyObject<'py>, T6: FromPyObject<'py>, T7: FromPyObject<'py>> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, 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>> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, 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>> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, 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>> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, 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>> FromPyObject<'py> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T> FromPyObject<'py> for Option<T>
where T: FromPyObject<'py>,

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T> FromPyObject<'py> for Vec<T>
where T: FromPyObject<'py>,

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T, const N: usize> FromPyObject<'py> for [T; N]
where T: FromPyObject<'py>,

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Source§

impl<'py, T: FromPyObject<'py>> FromPyObject<'py> for Cell<T>

Source§

fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self>

Implementors§

Source§

impl FromPyObject<'_> for PyBackedBytes

Source§

impl FromPyObject<'_> for PyBackedStr

Source§

impl<'py, T> FromPyObject<'py> for Bound<'py, T>
where T: PyTypeCheck,

Source§

impl<'py, T> FromPyObject<'py> for PyRef<'py, T>
where T: PyClass,

Source§

impl<'py, T> FromPyObject<'py> for PyRefMut<'py, T>
where T: PyClass<Frozen = False>,

Source§

impl<'py, T: Element> FromPyObject<'py> for PyBuffer<T>

Source§

impl<T> FromPyObject<'_> for Py<T>
where T: PyTypeCheck,

Source§

impl<T> FromPyObject<'_> for T
where T: PyClass + Clone,