diff --git a/src/types/function.rs b/src/types/function.rs index 4772e5aaf04..fb31b087f29 100644 --- a/src/types/function.rs +++ b/src/types/function.rs @@ -21,14 +21,13 @@ unsafe extern "C" fn run_closure( kwargs: *mut ffi::PyObject, ) -> *mut ffi::PyObject where - F: FnMut(&types::PyTuple, Option<&types::PyDict>) -> R + Send + 'static, + F: Fn(&types::PyTuple, Option<&types::PyDict>) -> R + Send + 'static, R: crate::callback::IntoPyCallbackOutput<*mut ffi::PyObject>, { crate::callback_body!(py, { - let boxed_fn: &mut F = &mut *(ffi::PyCapsule_GetPointer( - capsule_ptr, - CLOSURE_CAPSULE_NAME.as_ptr() as *const _, - ) as *mut F); + let boxed_fn: &F = + &*(ffi::PyCapsule_GetPointer(capsule_ptr, CLOSURE_CAPSULE_NAME.as_ptr() as *const _) + as *mut F); let args = py.from_borrowed_ptr::(args); let kwargs = py.from_borrowed_ptr_or_opt::(kwargs); boxed_fn(args, kwargs) @@ -37,7 +36,7 @@ where unsafe extern "C" fn drop_closure(capsule_ptr: *mut ffi::PyObject) where - F: FnMut(&types::PyTuple, Option<&types::PyDict>) -> R + Send + 'static, + F: Fn(&types::PyTuple, Option<&types::PyDict>) -> R + Send + 'static, R: crate::callback::IntoPyCallbackOutput<*mut ffi::PyObject>, { let result = std::panic::catch_unwind(|| { @@ -102,7 +101,7 @@ impl PyCFunction { /// ``` pub fn new_closure(f: F, py: Python) -> PyResult<&PyCFunction> where - F: FnMut(&types::PyTuple, Option<&types::PyDict>) -> R + Send + 'static, + F: Fn(&types::PyTuple, Option<&types::PyDict>) -> R + Send + 'static, R: crate::callback::IntoPyCallbackOutput<*mut ffi::PyObject>, { let function_ptr = Box::into_raw(Box::new(f)); diff --git a/tests/test_pyfunction.rs b/tests/test_pyfunction.rs index 2e7437eb61b..2fe675b4c60 100644 --- a/tests/test_pyfunction.rs +++ b/tests/test_pyfunction.rs @@ -254,9 +254,10 @@ fn test_closure_counter() { let gil = Python::acquire_gil(); let py = gil.python(); - let mut counter = Box::new(0); + let counter = std::cell::RefCell::new(0); let counter_fn = move |_args: &types::PyTuple, _kwargs: Option<&types::PyDict>| -> PyResult { + let mut counter = counter.borrow_mut(); *counter += 1; Ok(*counter) };