From f1eac51ea3f30bb6e70e35efaeebd4d28818a2c2 Mon Sep 17 00:00:00 2001 From: fbrouille Date: Thu, 7 Sep 2023 12:32:40 +0200 Subject: [PATCH] add as_ptr() and from_glib_ptr_borrow_mut() to TypeInfo, InterfaceInfo, TypeValueTable Signed-off-by: fbrouille --- glib/src/gobject/interface_info.rs | 22 +++++++++++++++++++++- glib/src/gobject/type_info.rs | 20 +++++++++++++++++++- glib/src/gobject/type_module.rs | 4 ++-- glib/src/gobject/type_plugin.rs | 6 +++--- glib/src/gobject/type_value_table.rs | 22 +++++++++++++++++++++- glib/src/subclass/type_plugin.rs | 12 ++++++------ glib/src/subclass/types.rs | 4 ++-- 7 files changed, 74 insertions(+), 16 deletions(-) diff --git a/glib/src/gobject/interface_info.rs b/glib/src/gobject/interface_info.rs index b943ee8f9ddd..50307a376263 100644 --- a/glib/src/gobject/interface_info.rs +++ b/glib/src/gobject/interface_info.rs @@ -3,4 +3,24 @@ #[derive(Debug)] #[doc(alias = "GInterfaceInfo")] #[repr(transparent)] -pub struct InterfaceInfo(pub gobject_ffi::GInterfaceInfo); +pub struct InterfaceInfo(pub(crate) gobject_ffi::GInterfaceInfo); + +impl InterfaceInfo { + // rustdoc-stripper-ignore-next + /// Returns a `GInterfaceInfo` pointer. + #[doc(hidden)] + #[inline] + pub fn as_ptr(&self) -> *mut gobject_ffi::GInterfaceInfo { + &self.0 as *const gobject_ffi::GInterfaceInfo as *mut _ + } + + // rustdoc-stripper-ignore-next + /// Borrows the underlying C value mutably. + #[doc(hidden)] + #[inline] + pub unsafe fn from_glib_ptr_borrow_mut<'a>( + ptr: *mut gobject_ffi::GInterfaceInfo, + ) -> &'a mut Self { + &mut *(ptr as *mut Self) + } +} diff --git a/glib/src/gobject/type_info.rs b/glib/src/gobject/type_info.rs index 86fb8a655fe6..e45877367f8d 100644 --- a/glib/src/gobject/type_info.rs +++ b/glib/src/gobject/type_info.rs @@ -3,4 +3,22 @@ #[derive(Debug)] #[doc(alias = "GTypeInfo")] #[repr(transparent)] -pub struct TypeInfo(pub gobject_ffi::GTypeInfo); +pub struct TypeInfo(pub(crate) gobject_ffi::GTypeInfo); + +impl TypeInfo { + // rustdoc-stripper-ignore-next + /// Returns a `GTypeInfo` pointer. + #[doc(hidden)] + #[inline] + pub fn as_ptr(&self) -> *mut gobject_ffi::GTypeInfo { + &self.0 as *const gobject_ffi::GTypeInfo as *mut _ + } + + // rustdoc-stripper-ignore-next + /// Borrows the underlying C value mutably. + #[doc(hidden)] + #[inline] + pub unsafe fn from_glib_ptr_borrow_mut<'a>(ptr: *mut gobject_ffi::GTypeInfo) -> &'a mut Self { + &mut *(ptr as *mut Self) + } +} diff --git a/glib/src/gobject/type_module.rs b/glib/src/gobject/type_module.rs index 6a97d0763255..2bc6224d4938 100644 --- a/glib/src/gobject/type_module.rs +++ b/glib/src/gobject/type_module.rs @@ -38,7 +38,7 @@ pub trait TypeModuleExt: IsA + sealed::Sealed + 'static { self.as_ref().to_glib_none().0, instance_type.into_glib(), interface_type.into_glib(), - &interface_info.0 as *const _ as *mut _, + interface_info.as_ptr(), ); } } @@ -78,7 +78,7 @@ pub trait TypeModuleExt: IsA + sealed::Sealed + 'static { self.as_ref().to_glib_none().0, parent_type.into_glib(), type_name.to_glib_none().0, - &type_info.0 as *const _ as *mut _, + type_info.as_ptr(), flags.into_glib(), )) } diff --git a/glib/src/gobject/type_plugin.rs b/glib/src/gobject/type_plugin.rs index ffa1b315cbfc..80a324024895 100644 --- a/glib/src/gobject/type_plugin.rs +++ b/glib/src/gobject/type_plugin.rs @@ -36,7 +36,7 @@ pub trait TypePluginExt: IsA + sealed::Sealed + 'static { self.as_ref().to_glib_none().0, instance_type.into_glib(), interface_type.into_glib(), - &info.0 as *const _ as *mut _, + info.as_ptr(), ); } } @@ -52,8 +52,8 @@ pub trait TypePluginExt: IsA + sealed::Sealed + 'static { gobject_ffi::g_type_plugin_complete_type_info( self.as_ref().to_glib_none().0, g_type.into_glib(), - &info.0 as *const _ as *mut _, - &value_table.0 as *const _ as *mut _, + info.as_ptr(), + value_table.as_ptr(), ); } } diff --git a/glib/src/gobject/type_value_table.rs b/glib/src/gobject/type_value_table.rs index 0f8424a4ee55..dc5adb50a346 100644 --- a/glib/src/gobject/type_value_table.rs +++ b/glib/src/gobject/type_value_table.rs @@ -3,4 +3,24 @@ #[derive(Debug)] #[doc(alias = "GTypeValueTable")] #[repr(transparent)] -pub struct TypeValueTable(pub gobject_ffi::GTypeValueTable); +pub struct TypeValueTable(pub(crate) gobject_ffi::GTypeValueTable); + +impl TypeValueTable { + // rustdoc-stripper-ignore-next + /// Returns a `GTypeValueTable` pointer. + #[doc(hidden)] + #[inline] + pub fn as_ptr(&self) -> *mut gobject_ffi::GTypeValueTable { + &self.0 as *const gobject_ffi::GTypeValueTable as *mut _ + } + + // rustdoc-stripper-ignore-next + /// Borrows the underlying C value mutably. + #[doc(hidden)] + #[inline] + pub unsafe fn from_glib_ptr_borrow_mut<'a>( + ptr: *mut gobject_ffi::GTypeValueTable, + ) -> &'a mut Self { + &mut *(ptr as *mut Self) + } +} diff --git a/glib/src/subclass/type_plugin.rs b/glib/src/subclass/type_plugin.rs index 5b5bcc98d635..5cf97bf3f957 100644 --- a/glib/src/subclass/type_plugin.rs +++ b/glib/src/subclass/type_plugin.rs @@ -99,8 +99,8 @@ impl TypePluginImplExt for T { f( self.obj().unsafe_cast_ref::().to_glib_none().0, type_.into_glib(), - &info.0 as *const _ as *mut _, - &value_table.0 as *const _ as *mut _, + info.as_ptr(), + value_table.as_ptr(), ) } } @@ -124,7 +124,7 @@ impl TypePluginImplExt for T { self.obj().unsafe_cast_ref::().to_glib_none().0, instance_type.into_glib(), interface_type.into_glib(), - &info.0 as *const _ as *mut _, + info.as_ptr(), ) } } @@ -164,8 +164,8 @@ unsafe extern "C" fn complete_type_info( let instance = &*(type_plugin as *mut T::Instance); let imp = instance.imp(); let type_ = Type::from_glib(gtype); - let info: &mut TypeInfo = &mut *(info_ptr as *mut _); - let value_table: &mut TypeValueTable = &mut *(value_table_ptr as *mut _); + let info = TypeInfo::from_glib_ptr_borrow_mut(info_ptr); + let value_table = TypeValueTable::from_glib_ptr_borrow_mut(value_table_ptr); imp.complete_type_info(type_, info, value_table); } @@ -180,7 +180,7 @@ unsafe extern "C" fn complete_interface_info( let imp = instance.imp(); let instance_type = Type::from_glib(instance_gtype); let interface_type = Type::from_glib(interface_gtype); - let info: &mut InterfaceInfo = &mut *(info_ptr as *mut _); + let info = InterfaceInfo::from_glib_ptr_borrow_mut(info_ptr); imp.complete_interface_info(instance_type, interface_type, info); } diff --git a/glib/src/subclass/types.rs b/glib/src/subclass/types.rs index 097a33285195..934d457de8ef 100644 --- a/glib/src/subclass/types.rs +++ b/glib/src/subclass/types.rs @@ -1043,7 +1043,7 @@ pub fn register_type() -> Type { gobject_ffi::g_type_add_interface_static( type_.into_glib(), iface_type.into_glib(), - &iface_info.0 as *const _ as *mut _, + iface_info.as_ptr(), ); } @@ -1136,7 +1136,7 @@ pub fn register_module_type(type_module: &TypeModule) -> Type gobject_ffi::g_type_add_interface_static( type_.into_glib(), iface_type.into_glib(), - &iface_info.0 as *const _ as *mut _, + iface_info.as_ptr(), ); } }