From 68c1932e5e914e0ad6e2a277fc9a072c4b4f9911 Mon Sep 17 00:00:00 2001 From: laurent Date: Thu, 14 Oct 2021 05:51:03 +0100 Subject: [PATCH] Revert the FnMut changes. --- src/types/function.rs | 13 ++++++------- tests/test_pyfunction.rs | 3 ++- 2 files changed, 8 insertions(+), 8 deletions(-) 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) };