Skip to main content

PyFunctionArgument

Trait PyFunctionArgument 

Source
pub trait PyFunctionArgument<'a, 'holder, 'py, const IMPLEMENTS_FROMPYOBJECT: bool>: Sized + Sealed<IMPLEMENTS_FROMPYOBJECT> {
    type Holder: FunctionArgumentHolder;
    type Error: Into<PyErr>;

    const INPUT_TYPE: PyStaticExpr;

    // Required method
    fn extract(
        obj: Borrowed<'a, 'py, PyAny>,
        holder: &'holder mut Self::Holder,
    ) -> Result<Self, Self::Error>;
}
Expand description

A trait which is used to help PyO3 macros extract function arguments.

#[pyclass] structs need to extract as PyRef<T> and PyRefMut<T> wrappers rather than extracting &T and &mut T directly. The Holder type is used to hold these temporary wrappers - the way the macro is constructed, these wrappers will be dropped as soon as the pyfunction call ends.

There exists a trivial blanket implementation for T: FromPyObject with Holder = ().

The const generic arg IMPLEMENTS_FROMPYOBJECT allows for const generic specialization of some additional types which don’t implement FromPyObject, such as &T for #[pyclass] types. All types should only implement this trait once; either by the FromPyObject blanket or one of the specialized implementations which needs a Holder.

Required Associated Constants§

Source

const INPUT_TYPE: PyStaticExpr

Available on crate feature experimental-inspect only.

Provides the type hint information for which Python types are allowed.

Required Associated Types§

Required Methods§

Source

fn extract( obj: Borrowed<'a, 'py, PyAny>, holder: &'holder mut Self::Holder, ) -> Result<Self, Self::Error>

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<'a, 'holder, 'py, T> PyFunctionArgument<'a, 'holder, 'py, false> for Option<T>
where T: PyFunctionArgument<'a, 'holder, 'py, false>,

Allow Option<T> to be a function argument also for types which don’t implement FromPyObject

Source§

const INPUT_TYPE: PyStaticExpr

Available on crate feature experimental-inspect only.
Source§

type Holder = <T as PyFunctionArgument<'a, 'holder, 'py, false>>::Holder

Source§

type Error = <T as PyFunctionArgument<'a, 'holder, 'py, false>>::Error

Source§

fn extract( obj: Borrowed<'a, 'py, PyAny>, holder: &'holder mut T::Holder, ) -> Result<Self, Self::Error>

Source§

impl<'a, 'holder, T: PyClass> PyFunctionArgument<'a, 'holder, '_, false> for &'holder T

Source§

const INPUT_TYPE: PyStaticExpr = T::TYPE_HINT

Available on crate feature experimental-inspect only.
Source§

type Holder = Option<PyClassGuard<'a, T>>

Source§

type Error = PyErr

Source§

fn extract( obj: Borrowed<'a, '_, PyAny>, holder: &'holder mut Self::Holder, ) -> PyResult<Self>

Source§

impl<'a, 'holder, T: PyClass<Frozen = False>> PyFunctionArgument<'a, 'holder, '_, false> for &'holder mut T

Source§

const INPUT_TYPE: PyStaticExpr = T::TYPE_HINT

Available on crate feature experimental-inspect only.
Source§

type Holder = Option<PyClassGuardMut<'a, T>>

Source§

type Error = PyErr

Source§

fn extract( obj: Borrowed<'a, '_, PyAny>, holder: &'holder mut Self::Holder, ) -> PyResult<Self>

Implementors§

Source§

impl<'a, 'holder, 'py, T> PyFunctionArgument<'a, 'holder, 'py, false> for &'holder Bound<'py, T>
where T: PyTypeCheck + 'a + 'py,

Source§

const INPUT_TYPE: PyStaticExpr = T::TYPE_HINT

Source§

type Holder = Option<Borrowed<'a, 'py, T>>

Source§

type Error = CastError<'a, 'py>

Source§

impl<'a, 'py, T> PyFunctionArgument<'a, '_, 'py, true> for T
where T: FromPyObject<'a, 'py>,

Source§

const INPUT_TYPE: PyStaticExpr = T::INPUT_TYPE

Source§

type Holder = ()

Source§

type Error = <T as FromPyObject<'a, 'py>>::Error

⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here