From 0040bb0f96ded542367f58e81eddc5870545b1cb Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Wed, 10 Jul 2024 12:39:46 +0200 Subject: [PATCH] gtk: Relax Class methods trait requirements So they can be called outside of class_init unless required in which case we add an explicit Self: ClassStruct requirement --- gtk4/src/subclass/cell_area.rs | 4 ++-- gtk4/src/subclass/widget.rs | 43 ++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/gtk4/src/subclass/cell_area.rs b/gtk4/src/subclass/cell_area.rs index 8e50c3423a1f..5609fd93797d 100644 --- a/gtk4/src/subclass/cell_area.rs +++ b/gtk4/src/subclass/cell_area.rs @@ -930,7 +930,7 @@ unsafe extern "C" fn cell_area_foreach_alloc( } #[allow(clippy::missing_safety_doc)] -pub unsafe trait CellAreaClassExt: ClassStruct { +pub unsafe trait CellAreaClassExt { #[doc(alias = "gtk_cell_area_class_find_cell_property")] fn find_cell_property(&self, property_name: &str) -> Option { unsafe { @@ -956,4 +956,4 @@ pub unsafe trait CellAreaClassExt: ClassStruct { } } -unsafe impl CellAreaClassExt for T where T::Type: CellAreaImpl {} +unsafe impl + glib::object::IsClass> CellAreaClassExt for glib::object::Class {} diff --git a/gtk4/src/subclass/widget.rs b/gtk4/src/subclass/widget.rs index a771c73aeac5..cb111aaf6a35 100644 --- a/gtk4/src/subclass/widget.rs +++ b/gtk4/src/subclass/widget.rs @@ -861,7 +861,7 @@ unsafe extern "C" fn widget_unroot(ptr: *mut ffi::GtkWidget) { } #[allow(clippy::missing_safety_doc)] -pub unsafe trait WidgetClassExt: ClassStruct { +pub unsafe trait WidgetClassExt { #[doc(alias = "gtk_widget_class_set_template")] fn set_template_bytes(&mut self, template: &glib::Bytes) { unsafe { @@ -905,6 +905,7 @@ pub unsafe trait WidgetClassExt: ClassStruct { + 'static + Clone, Fut: Future, + Self: ClassStruct, { self.install_action( action_name, @@ -939,6 +940,7 @@ pub unsafe trait WidgetClassExt: ClassStruct { ) where F: Fn(&<::Type as ObjectSubclass>::Type, &str, Option<&Variant>) + 'static, + Self: ClassStruct, { unsafe { // We store the activate callbacks in a HashMap @@ -1024,7 +1026,9 @@ pub unsafe trait WidgetClassExt: ClassStruct { keyval: gdk::Key, mods: gdk::ModifierType, callback: F, - ) { + ) where + Self: ClassStruct, + { let shortcut = crate::Shortcut::new( Some(crate::KeyvalTrigger::new(keyval, mods)), Some(crate::CallbackAction::new( @@ -1046,8 +1050,11 @@ pub unsafe trait WidgetClassExt: ClassStruct { } #[doc(alias = "gtk_widget_class_add_binding_signal")] - fn add_binding_signal(&mut self, keyval: gdk::Key, mods: gdk::ModifierType, signal_name: &str) { - let type_ = ::type_(); + fn add_binding_signal(&mut self, keyval: gdk::Key, mods: gdk::ModifierType, signal_name: &str) + where + Self: ClassStruct, + { + let type_ = <::Type as ObjectSubclassType>::type_(); assert!( SignalId::lookup(signal_name, type_).is_some(), "Signal '{signal_name}' doesn't exists for type '{type_}'", @@ -1103,8 +1110,11 @@ pub unsafe trait WidgetClassExt: ClassStruct { } #[doc(alias = "gtk_widget_class_set_activate_signal_from_name")] - fn set_activate_signal_from_name(&mut self, signal_name: &str) { - let type_ = ::type_(); + fn set_activate_signal_from_name(&mut self, signal_name: &str) + where + Self: ClassStruct, + { + let type_ = <::Type as ObjectSubclassType>::type_(); assert!( SignalId::lookup(signal_name, type_).is_some(), "Signal '{signal_name}' doesn't exists for type '{type_}'", @@ -1179,12 +1189,13 @@ pub unsafe trait WidgetClassExt: ClassStruct { &mut self, name: &str, internal: bool, - offset: field_offset::FieldOffset>, + offset: field_offset::FieldOffset<::Type, TemplateChild>, ) where T: ObjectType + FromGlibPtrNone<*mut ::GlibType>, + Self: ClassStruct, { let widget_class = self as *mut _ as *mut ffi::GtkWidgetClass; - let private_offset = ::type_data() + let private_offset = <::Type as ObjectSubclassType>::type_data() .as_ref() .impl_offset(); ffi::gtk_widget_class_bind_template_child_full( @@ -1195,10 +1206,13 @@ pub unsafe trait WidgetClassExt: ClassStruct { ) } - fn rust_template_scope(&mut self) -> BuilderRustScope { + fn rust_template_scope(&mut self) -> BuilderRustScope + where + Self: ClassStruct, + { assert_initialized_main_thread!(); unsafe { - let mut data = ::type_data(); + let mut data = <::Type as ObjectSubclassType>::type_data(); let internal = data .as_mut() .class_data_mut::(::type_()) @@ -1213,7 +1227,7 @@ pub unsafe trait WidgetClassExt: ClassStruct { } } -unsafe impl WidgetClassExt for T where T::Type: WidgetImpl {} +unsafe impl + glib::object::IsClass> WidgetClassExt for glib::object::Class {} #[derive(Debug, PartialEq, Eq)] #[repr(transparent)] @@ -1354,13 +1368,16 @@ pub trait CompositeTemplateCallbacks { // rustdoc-stripper-ignore-next /// Binds the template callbacks from this type into the default template /// scope for `klass`. - fn bind_template_callbacks(klass: &mut T) { + fn bind_template_callbacks(klass: &mut T) { Self::add_callbacks_to_scope(&klass.rust_template_scope()); } // rustdoc-stripper-ignore-next /// Binds the template callbacks from this type into the default template /// scope for `klass`, prepending `prefix` to each callback name. - fn bind_template_callbacks_prefixed(klass: &mut T, prefix: &str) { + fn bind_template_callbacks_prefixed( + klass: &mut T, + prefix: &str, + ) { Self::add_callbacks_to_scope_prefixed(&klass.rust_template_scope(), prefix); } // rustdoc-stripper-ignore-next