pyo3::conversion

Trait IntoPy

Source
pub trait IntoPy<T>: Sized {
    // Required method
    fn into_py(self, py: Python<'_>) -> T;
}
Expand description

Defines a conversion from a Rust type to a Python object.

It functions similarly to std’s Into trait, but requires a GIL token as an argument. Many functions and traits internal to PyO3 require this trait as a bound, so a lack of this trait can manifest itself in different error messages.

§Examples

§With #[pyclass]

The easiest way to implement IntoPy is by exposing a struct as a native Python object by annotating it with #[pyclass].

use pyo3::prelude::*;

#[pyclass]
struct Number {
    #[pyo3(get, set)]
    value: i32,
}

Python code will see this as an instance of the Number class with a value attribute.

§Conversion to a Python object

However, it may not be desirable to expose the existence of Number to Python code. IntoPy allows us to define a conversion to an appropriate Python object.

use pyo3::prelude::*;

struct Number {
    value: i32,
}

impl IntoPy<PyObject> for Number {
    fn into_py(self, py: Python<'_>) -> PyObject {
        // delegates to i32's IntoPy implementation.
        self.value.into_py(py)
    }
}

Python code will see this as an int object.

§Dynamic conversion into Python objects.

It is also possible to return a different Python object depending on some condition. This is useful for types like enums that can carry different types.

use pyo3::prelude::*;

enum Value {
    Integer(i32),
    String(String),
    None,
}

impl IntoPy<PyObject> for Value {
    fn into_py(self, py: Python<'_>) -> PyObject {
        match self {
            Self::Integer(val) => val.into_py(py),
            Self::String(val) => val.into_py(py),
            Self::None => py.None(),
        }
    }
}

Python code will see this as any of the int, string or None objects.

Required Methods§

Source

fn into_py(self, py: Python<'_>) -> T

Performs the conversion.

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 IntoPy<Py<PyAny>> for &OsStr

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for Cow<'_, str>

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for Cow<'_, OsStr>

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for Cow<'_, [u8]>

Source§

fn into_py(self, py: Python<'_>) -> Py<PyAny>

Source§

impl IntoPy<Py<PyAny>> for IpAddr

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for bool

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for char

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for f32

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for f64

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for i8

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for i16

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for i32

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for i64

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for i128

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for isize

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for u8

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for u16

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for u32

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for u64

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for u128

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for ()

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for usize

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for String

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for Duration

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for OsString

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for PathBuf

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for SystemTime

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroI8

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroI16

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroI32

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroI64

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroI128

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroIsize

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroU8

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroU16

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroU32

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroU64

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroU128

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyAny>> for NonZeroUsize

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl IntoPy<Py<PyTuple>> for ()

Converts () to an empty Python tuple.

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<'a> IntoPy<Py<PyAny>> for &'a str

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<'a> IntoPy<Py<PyAny>> for &'a String

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<'a> IntoPy<Py<PyAny>> for &'a OsString

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<'a> IntoPy<Py<PyAny>> for &'a Path

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<'a> IntoPy<Py<PyAny>> for &'a PathBuf

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<'a> IntoPy<Py<PyAny>> for &'a [u8]

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<'a> IntoPy<Py<PyAny>> for Cow<'a, Path>

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<'a> IntoPy<Py<PyString>> for &'a str

Source§

fn into_py(self, py: Python<'_>) -> Py<PyString>

Source§

impl<K> IntoPy<Py<PyAny>> for BTreeSet<K>
where K: IntoPy<PyObject> + Ord,

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<K, S> IntoPy<Py<PyAny>> for HashSet<K, S>
where K: IntoPy<PyObject> + Eq + Hash, S: BuildHasher + Default,

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<K, V> IntoPy<Py<PyAny>> for BTreeMap<K, V>
where K: Eq + IntoPy<PyObject>, V: IntoPy<PyObject>,

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<K, V, H> IntoPy<Py<PyAny>> for HashMap<K, V, H>
where K: Hash + Eq + IntoPy<PyObject>, V: IntoPy<PyObject>, H: BuildHasher,

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0,)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0,)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>, T10: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>, T10: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>, T10: IntoPy<PyObject>, T11: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>, T10: IntoPy<PyObject>, T11: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

Source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

Source§

impl<T> IntoPy<Py<PyAny>> for Option<T>
where T: IntoPy<PyObject>,

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T> IntoPy<Py<PyAny>> for &T
where T: AsRef<PyAny>,

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T> IntoPy<Py<PyAny>> for Vec<T>
where T: IntoPy<PyObject>,

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T, const N: usize> IntoPy<Py<PyAny>> for [T; N]
where T: IntoPy<PyObject>,

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Source§

impl<T: Copy + IntoPy<PyObject>> IntoPy<Py<PyAny>> for Cell<T>

Source§

fn into_py(self, py: Python<'_>) -> PyObject

Implementors§

Source§

impl IntoPy<Py<PyAny>> for &PyAny

Source§

impl IntoPy<Py<PyAny>> for PyErr

Source§

impl IntoPy<Py<PyAny>> for PyBackedBytes

Source§

impl IntoPy<Py<PyAny>> for PyBackedStr

Source§

impl IntoPy<Py<PyString>> for &Bound<'_, PyString>

Source§

impl IntoPy<Py<PyString>> for &Py<PyString>

Source§

impl IntoPy<Py<PyString>> for Bound<'_, PyString>

Source§

impl IntoPy<Py<PyTuple>> for &Bound<'_, PyTuple>

Source§

impl IntoPy<Py<PyTuple>> for Bound<'_, PyTuple>

Source§

impl<'a> IntoPy<Py<PyAny>> for &'a PyErr

Source§

impl<T> IntoPy<Py<PyAny>> for &Bound<'_, T>

Source§

impl<T> IntoPy<Py<PyAny>> for &Py<T>

Source§

impl<T> IntoPy<Py<PyAny>> for Borrowed<'_, '_, T>

Source§

impl<T> IntoPy<Py<PyAny>> for Bound<'_, T>

Source§

impl<T> IntoPy<Py<PyAny>> for Py<T>

Source§

impl<T: PyClass> IntoPy<Py<PyAny>> for &PyRef<'_, T>

Source§

impl<T: PyClass> IntoPy<Py<PyAny>> for PyRef<'_, T>

Source§

impl<T: PyClass<Frozen = False>> IntoPy<Py<PyAny>> for &PyRefMut<'_, T>

Source§

impl<T: PyClass<Frozen = False>> IntoPy<Py<PyAny>> for PyRefMut<'_, T>