From fe1fe5b1142a4b0a26ecef628f3eaccf4a2d4372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 30 Apr 2024 15:21:49 +0300 Subject: [PATCH] gstreamer: Use a reference to a pointer of correct mutability for from_glib_ptr_borrow() This hopefully makes it easier to use and harder to get the returned lifetime wrong. Part-of: --- gstreamer/src/miniobject.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/gstreamer/src/miniobject.rs b/gstreamer/src/miniobject.rs index 4bc8e6481..1b859a10f 100644 --- a/gstreamer/src/miniobject.rs +++ b/gstreamer/src/miniobject.rs @@ -30,10 +30,12 @@ macro_rules! mini_object_wrapper ( impl $name { #[inline] - pub unsafe fn from_glib_ptr_borrow<'a>( - ptr: *const *const $ffi_name, - ) -> &'a Self { - &*(ptr as *const $name) + pub unsafe fn from_glib_ptr_borrow( + ptr: &*mut $ffi_name, + ) -> &Self { + debug_assert_eq!(std::mem::size_of::<$name>(), std::mem::size_of::<$crate::glib::ffi::gpointer>()); + debug_assert!(!ptr.is_null()); + &*(ptr as *const *mut $ffi_name as *const $name) } #[inline] @@ -548,11 +550,8 @@ macro_rules! mini_object_wrapper ( #[inline] unsafe fn from_value(value: &'a $crate::glib::Value) -> Self { skip_assert_initialized!(); - assert_eq!(std::mem::size_of::<$name>(), std::mem::size_of::<$crate::glib::ffi::gpointer>()); let value = &*(value as *const $crate::glib::Value as *const $crate::glib::gobject_ffi::GValue); - let ptr = &value.data[0].v_pointer as *const $crate::glib::ffi::gpointer as *const *const $ffi_name; - debug_assert!(!(*ptr).is_null()); - &*(ptr as *const $name) + $name::from_glib_ptr_borrow(&*(&value.data[0].v_pointer as *const $crate::glib::ffi::gpointer as *const *mut $ffi_name)) } }