From 37edf441ec10fc08c713a547667cc0d860b1514c Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 6 May 2021 15:36:21 +0200 Subject: [PATCH] Expand wrap_pyfunction!() to a closure. The change to wrap_pyfunction!() #1143 makes it impossible to implement `context.add_wrapped(wrap_pyfunction!(something))` in `inline-python`. `context` does not carry the GIL lifetime, which causes type deduction trouble now that `wrap_pyfunction` results in a generic function. ``` error[E0308]: mismatched types --> examples/rust-fn.rs:12:4 | 12 | c.add_wrapped(wrap_pyfunction!(rust_print)); | ^^^^^^^^^^^ one type is more general than the other | = note: expected enum `Result<&pyo3::types::PyCFunction, _>` found enum `Result<&pyo3::types::PyCFunction, _>` ``` By re-wrapping the function as a closure, we trigger 'closure signature deduction' when passing `wrap_pyfunction!()` as an argument to a function: Rustc will deduce the signature of the closure from the function that closure is passed to. This way, the generic arguments can be deduced in more contexts, fixing the problem. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 62bf441f92a..92537de5a80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -221,7 +221,7 @@ pub mod proc_macro { #[macro_export] macro_rules! wrap_pyfunction { ($function_name: ident) => {{ - &pyo3::paste::expr! { [<__pyo3_get_function_ $function_name>] } + &|py| pyo3::paste::expr! { [<__pyo3_get_function_ $function_name>] }(py) }}; ($function_name: ident, $arg: expr) => {