From 38b6463a8b9ede2c050dc9ccd094535afa62d47e Mon Sep 17 00:00:00 2001 From: Fina Wilke Date: Tue, 24 Sep 2024 21:19:26 +0200 Subject: [PATCH] glib: Add additional type bounds to Impl traits --- glib/src/gobject/dynamic_object.rs | 1 + glib/src/subclass/type_module.rs | 30 ++++++++++++++++----- glib/src/subclass/type_plugin.rs | 43 +++++++++++++++++++++++------- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/glib/src/gobject/dynamic_object.rs b/glib/src/gobject/dynamic_object.rs index f6e7648046fb..8736aa8ec580 100644 --- a/glib/src/gobject/dynamic_object.rs +++ b/glib/src/gobject/dynamic_object.rs @@ -44,6 +44,7 @@ pub trait DynamicObjectRegisterExt: AsRef + sealed::Sealed + 'static impl + ObjectSubclassIsExt> DynamicObjectRegisterExt for O where O::Subclass: TypePluginRegisterImpl, + ::Type: IsA, { fn add_dynamic_interface( &self, diff --git a/glib/src/subclass/type_module.rs b/glib/src/subclass/type_module.rs index bb0fe6f78ce8..c93a22cbb167 100644 --- a/glib/src/subclass/type_module.rs +++ b/glib/src/subclass/type_module.rs @@ -2,7 +2,10 @@ use crate::{ffi, gobject_ffi, prelude::*, subclass::prelude::*, translate::*, TypeModule}; -pub trait TypeModuleImpl: ObjectImpl + TypeModuleImplExt { +pub trait TypeModuleImpl: ObjectImpl + TypeModuleImplExt +where + ::Type: IsA, +{ // rustdoc-stripper-ignore-next /// Loads the module, registers one or more object subclasses using /// [`register_dynamic_type`] and registers one or more object interfaces @@ -21,12 +24,18 @@ pub trait TypeModuleImpl: ObjectImpl + TypeModuleImplExt { fn unload(&self); } -pub trait TypeModuleImplExt: ObjectSubclass { +pub trait TypeModuleImplExt: ObjectSubclass +where + ::Type: IsA, +{ fn parent_load(&self) -> bool; fn parent_unload(&self); } -impl TypeModuleImplExt for T { +impl TypeModuleImplExt for T +where + ::Type: IsA, +{ fn parent_load(&self) -> bool { unsafe { let data = T::type_data(); @@ -58,7 +67,10 @@ impl TypeModuleImplExt for T { } } -unsafe impl IsSubclassable for TypeModule { +unsafe impl IsSubclassable for TypeModule +where + ::Type: IsA, +{ fn class_init(class: &mut crate::Class) { Self::parent_class_init::(class); @@ -70,7 +82,10 @@ unsafe impl IsSubclassable for TypeModule { unsafe extern "C" fn load( type_module: *mut gobject_ffi::GTypeModule, -) -> ffi::gboolean { +) -> ffi::gboolean +where + ::Type: IsA, +{ let instance = &*(type_module as *mut T::Instance); let imp = instance.imp(); @@ -90,7 +105,10 @@ unsafe extern "C" fn load( res.into_glib() } -unsafe extern "C" fn unload(type_module: *mut gobject_ffi::GTypeModule) { +unsafe extern "C" fn unload(type_module: *mut gobject_ffi::GTypeModule) +where + ::Type: IsA, +{ let instance = &*(type_module as *mut T::Instance); let imp = instance.imp(); diff --git a/glib/src/subclass/type_plugin.rs b/glib/src/subclass/type_plugin.rs index bb4d755f8a88..9700225d01b0 100644 --- a/glib/src/subclass/type_plugin.rs +++ b/glib/src/subclass/type_plugin.rs @@ -6,7 +6,10 @@ use crate::{ Type, TypeFlags, TypeInfo, TypePlugin, TypeValueTable, }; -pub trait TypePluginImpl: ObjectImpl + TypePluginImplExt { +pub trait TypePluginImpl: ObjectImpl + TypePluginImplExt +where + ::Type: IsA, +{ fn use_plugin(&self) { self.parent_use_plugin(); } @@ -24,7 +27,10 @@ pub trait TypePluginImpl: ObjectImpl + TypePluginImplExt { } } -pub trait TypePluginImplExt: ObjectSubclass { +pub trait TypePluginImplExt: ObjectSubclass +where + ::Type: IsA, +{ fn parent_use_plugin(&self); fn parent_unuse_plugin(&self); fn parent_complete_type_info(&self, type_: Type) -> (TypeInfo, TypeValueTable); @@ -35,7 +41,10 @@ pub trait TypePluginImplExt: ObjectSubclass { ) -> InterfaceInfo; } -impl TypePluginImplExt for T { +impl TypePluginImplExt for T +where + ::Type: IsA, +{ fn parent_use_plugin(&self) { unsafe { let type_data = Self::type_data(); @@ -113,7 +122,10 @@ impl TypePluginImplExt for T { } } -unsafe impl IsImplementable for TypePlugin { +unsafe impl IsImplementable for TypePlugin +where + ::Type: IsA, +{ fn interface_init(iface: &mut Interface) { let iface = iface.as_mut(); @@ -124,14 +136,20 @@ unsafe impl IsImplementable for TypePlugin { } } -unsafe extern "C" fn use_plugin(type_plugin: *mut gobject_ffi::GTypePlugin) { +unsafe extern "C" fn use_plugin(type_plugin: *mut gobject_ffi::GTypePlugin) +where + ::Type: IsA, +{ let instance = &*(type_plugin as *mut T::Instance); let imp = instance.imp(); imp.use_plugin(); } -unsafe extern "C" fn unuse_plugin(type_plugin: *mut gobject_ffi::GTypePlugin) { +unsafe extern "C" fn unuse_plugin(type_plugin: *mut gobject_ffi::GTypePlugin) +where + ::Type: IsA, +{ let instance = &*(type_plugin as *mut T::Instance); let imp = instance.imp(); @@ -143,7 +161,9 @@ unsafe extern "C" fn complete_type_info( gtype: ffi::GType, info_ptr: *mut gobject_ffi::GTypeInfo, value_table_ptr: *mut gobject_ffi::GTypeValueTable, -) { +) where + ::Type: IsA, +{ assert!(!info_ptr.is_null()); assert!(!value_table_ptr.is_null()); let instance = &*(type_plugin as *mut T::Instance); @@ -163,7 +183,9 @@ unsafe extern "C" fn complete_interface_info( instance_gtype: ffi::GType, interface_gtype: ffi::GType, info_ptr: *mut gobject_ffi::GInterfaceInfo, -) { +) where + ::Type: IsA, +{ assert!(!info_ptr.is_null()); let instance = &*(type_plugin as *mut T::Instance); let imp = instance.imp(); @@ -175,7 +197,10 @@ unsafe extern "C" fn complete_interface_info( *info = info_; } -pub trait TypePluginRegisterImpl: ObjectImpl + TypePluginImpl { +pub trait TypePluginRegisterImpl: ObjectImpl + TypePluginImpl +where + ::Type: IsA, +{ fn add_dynamic_interface( &self, _instance_type: Type,