Skip to content

Commit d1fdc2e

Browse files
sdroegebilelmoussaoui
authored andcommitted
glib: Implement object class methods via a trait instead of directly on Class<Object>
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<T>`.
1 parent 9125737 commit d1fdc2e

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

glib/Gir.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ manual = [
4747
"GLib.Variant",
4848
"GLib.VariantType",
4949
"GObject.Object",
50+
"GObject.ObjectClass",
5051
]
5152

5253
[[object]]

glib/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub use self::{
2222
enums::{EnumClass, EnumValue, FlagsBuilder, FlagsClass, FlagsValue, UserDirectory},
2323
error::{BoolError, Error},
2424
object::{
25-
BorrowedObject, Cast, CastNone, Class, InitiallyUnowned, Interface, IsA, Object, ObjectExt,
26-
ObjectType, SendWeakRef, WeakRef,
25+
BorrowedObject, Cast, CastNone, Class, InitiallyUnowned, Interface, IsA, Object,
26+
ObjectClassExt, ObjectExt, ObjectType, SendWeakRef, WeakRef,
2727
},
2828
signal::{
2929
signal_handler_block, signal_handler_disconnect, signal_handler_unblock,

glib/src/object.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,12 +3232,13 @@ fn validate_signal_arguments(type_: Type, signal_query: &SignalQuery, args: &mut
32323232
}
32333233
}
32343234

3235-
impl ObjectClass {
3235+
/// Trait for class methods on `Object` and subclasses of it.
3236+
pub unsafe trait ObjectClassExt {
32363237
// rustdoc-stripper-ignore-next
32373238
/// Check if the object class has a property `property_name` of the given `type_`.
32383239
///
32393240
/// If no type is provided then only the existence of the property is checked.
3240-
pub fn has_property(&self, property_name: &str, type_: Option<Type>) -> bool {
3241+
fn has_property(&self, property_name: &str, type_: Option<Type>) -> bool {
32413242
let ptype = self.property_type(property_name);
32423243

32433244
match (ptype, type_) {
@@ -3252,15 +3253,15 @@ impl ObjectClass {
32523253
///
32533254
/// This returns `None` if the property does not exist.
32543255
#[doc(alias = "get_property_type")]
3255-
pub fn property_type(&self, property_name: &str) -> Option<Type> {
3256+
fn property_type(&self, property_name: &str) -> Option<Type> {
32563257
self.find_property(property_name)
32573258
.map(|pspec| pspec.value_type())
32583259
}
32593260

32603261
// rustdoc-stripper-ignore-next
32613262
/// Get the [`ParamSpec`](crate::ParamSpec) of the property `property_name` of this object class.
32623263
#[doc(alias = "g_object_class_find_property")]
3263-
pub fn find_property(&self, property_name: &str) -> Option<crate::ParamSpec> {
3264+
fn find_property(&self, property_name: &str) -> Option<crate::ParamSpec> {
32643265
unsafe {
32653266
let klass = self as *const _ as *const gobject_ffi::GObjectClass;
32663267

@@ -3276,7 +3277,7 @@ impl ObjectClass {
32763277
// rustdoc-stripper-ignore-next
32773278
/// Return all [`ParamSpec`](crate::ParamSpec) of the properties of this object class.
32783279
#[doc(alias = "g_object_class_list_properties")]
3279-
pub fn list_properties(&self) -> PtrSlice<crate::ParamSpec> {
3280+
fn list_properties(&self) -> PtrSlice<crate::ParamSpec> {
32803281
unsafe {
32813282
let klass = self as *const _ as *const gobject_ffi::GObjectClass;
32823283

@@ -3289,6 +3290,8 @@ impl ObjectClass {
32893290
}
32903291
}
32913292

3293+
unsafe impl<T: ObjectType + IsClass> ObjectClassExt for Class<T> {}
3294+
32923295
wrapper! {
32933296
#[doc(alias = "GInitiallyUnowned")]
32943297
pub struct InitiallyUnowned(Object<gobject_ffi::GInitiallyUnowned, gobject_ffi::GInitiallyUnownedClass>);

glib/src/prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
//! Traits and essential types intended for blanket imports.
55
66
pub use crate::{
7-
param_spec::ParamSpecBuilderExt, Cast, CastNone, IsA, ObjectExt, ObjectType, ParamSpecType,
8-
StaticType, StaticTypeExt, StaticVariantType, ToSendValue, ToValue, ToVariant,
7+
param_spec::ParamSpecBuilderExt, Cast, CastNone, IsA, ObjectClassExt, ObjectExt, ObjectType,
8+
ParamSpecType, StaticType, StaticTypeExt, StaticVariantType, ToSendValue, ToValue, ToVariant,
99
};

0 commit comments

Comments
 (0)