From d1fdc2ea5797b5e091aec091f0389b163cce5a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 11 Oct 2023 15:36:47 +0300 Subject: [PATCH] glib: Implement object class methods via a trait instead of directly on `Class` This makes them callable directly from more places and is more in sync with how such methods are implemented in other crates that can't directly implement methods on `Class`. --- glib/Gir.toml | 1 + glib/src/lib.rs | 4 ++-- glib/src/object.rs | 13 ++++++++----- glib/src/prelude.rs | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/glib/Gir.toml b/glib/Gir.toml index f3702aedda51..674d744e360b 100644 --- a/glib/Gir.toml +++ b/glib/Gir.toml @@ -47,6 +47,7 @@ manual = [ "GLib.Variant", "GLib.VariantType", "GObject.Object", + "GObject.ObjectClass", ] [[object]] diff --git a/glib/src/lib.rs b/glib/src/lib.rs index 8daf84acfb57..0bc9e35b8e81 100644 --- a/glib/src/lib.rs +++ b/glib/src/lib.rs @@ -22,8 +22,8 @@ pub use self::{ enums::{EnumClass, EnumValue, FlagsBuilder, FlagsClass, FlagsValue, UserDirectory}, error::{BoolError, Error}, object::{ - BorrowedObject, Cast, CastNone, Class, InitiallyUnowned, Interface, IsA, Object, ObjectExt, - ObjectType, SendWeakRef, WeakRef, + BorrowedObject, Cast, CastNone, Class, InitiallyUnowned, Interface, IsA, Object, + ObjectClassExt, ObjectExt, ObjectType, SendWeakRef, WeakRef, }, signal::{ signal_handler_block, signal_handler_disconnect, signal_handler_unblock, diff --git a/glib/src/object.rs b/glib/src/object.rs index cfacf5382f80..c10fefe2bd8b 100644 --- a/glib/src/object.rs +++ b/glib/src/object.rs @@ -3232,12 +3232,13 @@ fn validate_signal_arguments(type_: Type, signal_query: &SignalQuery, args: &mut } } -impl ObjectClass { +/// Trait for class methods on `Object` and subclasses of it. +pub unsafe trait ObjectClassExt { // rustdoc-stripper-ignore-next /// Check if the object class has a property `property_name` of the given `type_`. /// /// If no type is provided then only the existence of the property is checked. - pub fn has_property(&self, property_name: &str, type_: Option) -> bool { + fn has_property(&self, property_name: &str, type_: Option) -> bool { let ptype = self.property_type(property_name); match (ptype, type_) { @@ -3252,7 +3253,7 @@ impl ObjectClass { /// /// This returns `None` if the property does not exist. #[doc(alias = "get_property_type")] - pub fn property_type(&self, property_name: &str) -> Option { + fn property_type(&self, property_name: &str) -> Option { self.find_property(property_name) .map(|pspec| pspec.value_type()) } @@ -3260,7 +3261,7 @@ impl ObjectClass { // rustdoc-stripper-ignore-next /// Get the [`ParamSpec`](crate::ParamSpec) of the property `property_name` of this object class. #[doc(alias = "g_object_class_find_property")] - pub fn find_property(&self, property_name: &str) -> Option { + fn find_property(&self, property_name: &str) -> Option { unsafe { let klass = self as *const _ as *const gobject_ffi::GObjectClass; @@ -3276,7 +3277,7 @@ impl ObjectClass { // rustdoc-stripper-ignore-next /// Return all [`ParamSpec`](crate::ParamSpec) of the properties of this object class. #[doc(alias = "g_object_class_list_properties")] - pub fn list_properties(&self) -> PtrSlice { + fn list_properties(&self) -> PtrSlice { unsafe { let klass = self as *const _ as *const gobject_ffi::GObjectClass; @@ -3289,6 +3290,8 @@ impl ObjectClass { } } +unsafe impl ObjectClassExt for Class {} + wrapper! { #[doc(alias = "GInitiallyUnowned")] pub struct InitiallyUnowned(Object); diff --git a/glib/src/prelude.rs b/glib/src/prelude.rs index 596442b2f40d..3ca66e4627bc 100644 --- a/glib/src/prelude.rs +++ b/glib/src/prelude.rs @@ -4,6 +4,6 @@ //! Traits and essential types intended for blanket imports. pub use crate::{ - param_spec::ParamSpecBuilderExt, Cast, CastNone, IsA, ObjectExt, ObjectType, ParamSpecType, - StaticType, StaticTypeExt, StaticVariantType, ToSendValue, ToValue, ToVariant, + param_spec::ParamSpecBuilderExt, Cast, CastNone, IsA, ObjectClassExt, ObjectExt, ObjectType, + ParamSpecType, StaticType, StaticTypeExt, StaticVariantType, ToSendValue, ToValue, ToVariant, };