diff --git a/src/types/capsule.rs b/src/types/capsule.rs index 9d4ad5197ce..15b47bb4575 100644 --- a/src/types/capsule.rs +++ b/src/types/capsule.rs @@ -85,7 +85,7 @@ impl PyCapsule { /// let capsule = PyCapsule::new(py, (), None).unwrap(); // Oops! `()` is zero sized! /// }); /// ``` - pub fn new( + pub fn new( py: Python<'_>, value: T, name: Option, @@ -100,16 +100,13 @@ impl PyCapsule { /// /// The `destructor` must be `Send`, because there is no guarantee which thread it will eventually /// be called from. - pub fn new_with_destructor< - T: 'static + Send + AssertNotZeroSized, - F: FnOnce(T, *mut c_void) + Send, - >( + pub fn new_with_destructor( py: Python<'_>, value: T, name: Option, destructor: F, ) -> PyResult> { - AssertNotZeroSized::assert_not_zero_sized(&value); + const { assert_not_zero_size::() } // Sanity check for capsule layout debug_assert_eq!(offset_of!(CapsuleContents::, value), 0); @@ -565,20 +562,14 @@ unsafe extern "C" fn capsule_destructor` -#[doc(hidden)] -pub trait AssertNotZeroSized: Sized { - const _CONDITION: usize = (std::mem::size_of::() == 0) as usize; - const _CHECK: &'static str = - ["PyCapsule value type T must not be zero-sized!"][Self::_CONDITION]; - #[allow(path_statements, clippy::no_effect)] - fn assert_not_zero_sized(&self) { - ::_CHECK; - } +#[track_caller] +const fn assert_not_zero_size() { + assert!( + size_of::() != 0, + "PyCapsule value type T must not be zero-sized!" + ) } -impl AssertNotZeroSized for T {} - fn ensure_no_error(py: Python<'_>) -> PyResult<()> { if let Some(err) = PyErr::take(py) { Err(err)