diff --git a/newsfragments/6049.fixed.md b/newsfragments/6049.fixed.md new file mode 100644 index 00000000000..6fb85866979 --- /dev/null +++ b/newsfragments/6049.fixed.md @@ -0,0 +1 @@ +Require `PyCapsule` destructors to be `'static`. diff --git a/src/types/capsule.rs b/src/types/capsule.rs index 798eb29f0be..12d2232c56c 100644 --- a/src/types/capsule.rs +++ b/src/types/capsule.rs @@ -196,7 +196,7 @@ impl PyCapsule { ) -> PyResult> where T: 'static + Send, - F: FnOnce(T, *mut c_void) + Send, + F: FnOnce(T, *mut c_void) + Send + 'static, { // Sanity check for capsule layout debug_assert_eq!(offset_of!(CapsuleContents::, value), 0); @@ -229,7 +229,7 @@ impl PyCapsule { since = "0.29.0", note = "use `PyCapsule::new_with_value_and_destructor` instead" )] - pub fn new_with_destructor( + pub fn new_with_destructor( py: Python<'_>, value: T, name: Option, @@ -652,7 +652,7 @@ impl CapsuleName { // C layout, as casting the capsule pointer to `T` depends on `T` being first. #[repr(C)] -struct CapsuleContents { +struct CapsuleContents { /// Value of the capsule value: T, /// Destructor to be used by the capsule @@ -663,7 +663,10 @@ struct CapsuleContents { } // Wrapping ffi::PyCapsule_Destructor for a user supplied FnOnce(T) for capsule destructor -unsafe extern "C" fn capsule_destructor( +unsafe extern "C" fn capsule_destructor< + T: 'static + Send, + F: FnOnce(T, *mut c_void) + Send + 'static, +>( capsule: *mut ffi::PyObject, ) { /// Gets the pointer and context from the capsule.