Skip to content

Commit

Permalink
allow from_py_with on function args to take a fn(&Bound) -> PyResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Feb 14, 2024
1 parent 0c12d91 commit 1a4a44d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,18 @@ fn impl_arg_param(
quote_arg_span! {
#[allow(clippy::redundant_closure)]
_pyo3::impl_::extract_argument::from_py_with_with_default(
#arg_value,
#arg_value.map(_pyo3::PyNativeType::as_borrowed).as_deref(),
#name_str,
#expr_path,
#expr_path as fn(_) -> _,
|| #default
)?
}
} else {
quote_arg_span! {
_pyo3::impl_::extract_argument::from_py_with(
_pyo3::impl_::extract_argument::unwrap_required_argument(#arg_value),
&_pyo3::impl_::extract_argument::unwrap_required_argument(#arg_value).as_borrowed(),
#name_str,
#expr_path,
#expr_path as fn(_) -> _,
)?
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/impl_/extract_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,23 @@ where

/// Alternative to [`extract_argument`] used when the argument has a `#[pyo3(from_py_with)]` annotation.
#[doc(hidden)]
pub fn from_py_with<'py, T>(
obj: &'py PyAny,
pub fn from_py_with<'a, 'py, T>(
obj: &'a Bound<'py, PyAny>,
arg_name: &str,
extractor: fn(&'py PyAny) -> PyResult<T>,
extractor: impl Into<super::frompyobject::Extractor<'a, 'py, T>>,
) -> PyResult<T> {
match extractor(obj) {
match extractor.into().call(obj) {
Ok(value) => Ok(value),
Err(e) => Err(argument_extraction_error(obj.py(), arg_name, e)),
}
}

/// Alternative to [`extract_argument`] used when the argument has a `#[pyo3(from_py_with)]` annotation and also a default value.
#[doc(hidden)]
pub fn from_py_with_with_default<'py, T>(
obj: Option<&'py PyAny>,
pub fn from_py_with_with_default<'a, 'py, T>(
obj: Option<&'a Bound<'py, PyAny>>,
arg_name: &str,
extractor: fn(&'py PyAny) -> PyResult<T>,
extractor: impl Into<super::frompyobject::Extractor<'a, 'py, T>>,
default: fn() -> T,
) -> PyResult<T> {
match obj {
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a, T> From<fn(&'a PyAny) -> PyResult<T>> for Extractor<'a, '_, T> {
}

impl<'a, 'py, T> Extractor<'a, 'py, T> {
fn call(self, obj: &'a Bound<'py, PyAny>) -> PyResult<T> {
pub(crate) fn call(self, obj: &'a Bound<'py, PyAny>) -> PyResult<T> {
match self {
Extractor::Bound(f) => f(obj),
Extractor::GilRef(f) => f(obj.as_gil_ref()),
Expand Down

0 comments on commit 1a4a44d

Please sign in to comment.