Trait IntoPyObject
pub trait IntoPyObject<'py>: Sized {
type Target;
type Output: BoundObject<'py, Self::Target>;
type Error: Into<PyErr>;
// Required method
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error>;
}tls only.Expand description
Defines a conversion from a Rust type to a Python object, which may fail.
This trait has #[derive(IntoPyObject)] to automatically implement it for simple types and
#[derive(IntoPyObjectRef)] to implement the same for references.
It functions similarly to std’s TryInto trait, but requires a Python<'py> token
as an argument.
The into_pyobject method is designed for maximum flexibility and efficiency; it
- allows for a concrete Python type to be returned (the
Targetassociated type) - allows for the smart pointer containing the Python object to be either
Bound<'py, Self::Target>orBorrowed<'a, 'py, Self::Target>to avoid unnecessary reference counting overhead - allows for a custom error type to be returned in the event of a conversion error to avoid unnecessarily creating a Python exception
§See also
- The
IntoPyObjectExttrait, which provides convenience methods for common usages ofIntoPyObjectwhich erase type information and convert errors toPyErr.
Required Associated Types§
type Target
type Target
The Python output type
type Output: BoundObject<'py, Self::Target>
type Output: BoundObject<'py, Self::Target>
The smart pointer type to use.
This will usually be Bound<'py, Target>, but in special cases Borrowed<'a, 'py, Target> can be
used to minimize reference counting overhead.
Required Methods§
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error>
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error>
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.