pyo3::conversion

Trait FromPyObjectBound

source
pub trait FromPyObjectBound<'a, 'py>: Sized + Sealed {
    // Required method
    fn from_py_object_bound(ob: Borrowed<'a, 'py, PyAny>) -> PyResult<Self>;

    // Provided method
    fn type_input() -> TypeInfo { ... }
}
Expand description

Expected form of FromPyObject to be used in a future PyO3 release.

The difference between this and FromPyObject is that this trait takes an additional lifetime 'a, which is the lifetime of the input Bound.

This allows implementations for &'a str and &'a [u8], which could not be expressed by the existing FromPyObject trait once the GIL Refs API was removed.

§Usage

Users are prevented from implementing this trait, instead they should implement the normal FromPyObject trait. This trait has a blanket implementation for T: FromPyObject.

The only case where this trait may have a use case to be implemented is when the lifetime of the extracted value is tied to the lifetime 'a of the input Bound instead of the GIL lifetime py, as is the case for the &'a str implementation.

Please contact the PyO3 maintainers if you believe you have a use case for implementing this trait before PyO3 is ready to change the main FromPyObject trait to take an additional lifetime.

Similarly, users should typically not call these trait methods and should instead use this via the extract method on Bound and Py.

Required Methods§

source

fn from_py_object_bound(ob: Borrowed<'a, 'py, PyAny>) -> PyResult<Self>

Extracts Self from the bound smart pointer obj.

Users are advised against calling this method directly: instead, use this via [Bound<'_, PyAny>::extract] or Py::extract.

Provided Methods§

source

fn type_input() -> TypeInfo

Extracts the type hint information for this type when it appears as an argument.

For example, Vec<u32> would return Sequence[int]. The default implementation returns Any, which is correct for any type.

For most types, the return value for this method will be identical to that of IntoPy::type_output. It may be different for some types, such as Dict, to allow duck-typing: functions return Dict but take Mapping as argument.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'a> FromPyObjectBound<'a, '_> for &'a str

source§

impl<'a> FromPyObjectBound<'a, '_> for &'a [u8]

source§

impl<'a> FromPyObjectBound<'a, '_> for Cow<'a, str>

source§

impl<'a> FromPyObjectBound<'a, '_> for Cow<'a, [u8]>

Special-purpose trait impl to efficiently handle both bytes and bytearray

If the source object is a bytes object, the Cow will be borrowed and pointing into the source object, and no copying or heap allocations will happen. If it is a bytearray, its contents will be copied to an owned Cow.

Implementors§

source§

impl<'py, T> FromPyObjectBound<'_, 'py> for T
where T: FromPyObject<'py>,