Skip to content

Commit

Permalink
glib: Implement object class methods via a trait instead of directly …
Browse files Browse the repository at this point in the history
…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>`.
  • Loading branch information
sdroege authored and bilelmoussaoui committed Oct 11, 2023
1 parent 9125737 commit d1fdc2e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions glib/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ manual = [
"GLib.Variant",
"GLib.VariantType",
"GObject.Object",
"GObject.ObjectClass",
]

[[object]]
Expand Down
4 changes: 2 additions & 2 deletions glib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 8 additions & 5 deletions glib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type>) -> bool {
fn has_property(&self, property_name: &str, type_: Option<Type>) -> bool {
let ptype = self.property_type(property_name);

match (ptype, type_) {
Expand All @@ -3252,15 +3253,15 @@ 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<Type> {
fn property_type(&self, property_name: &str) -> Option<Type> {
self.find_property(property_name)
.map(|pspec| pspec.value_type())
}

// 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<crate::ParamSpec> {
fn find_property(&self, property_name: &str) -> Option<crate::ParamSpec> {
unsafe {
let klass = self as *const _ as *const gobject_ffi::GObjectClass;

Expand All @@ -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<crate::ParamSpec> {
fn list_properties(&self) -> PtrSlice<crate::ParamSpec> {
unsafe {
let klass = self as *const _ as *const gobject_ffi::GObjectClass;

Expand All @@ -3289,6 +3290,8 @@ impl ObjectClass {
}
}

unsafe impl<T: ObjectType + IsClass> ObjectClassExt for Class<T> {}

wrapper! {
#[doc(alias = "GInitiallyUnowned")]
pub struct InitiallyUnowned(Object<gobject_ffi::GInitiallyUnowned, gobject_ffi::GInitiallyUnownedClass>);
Expand Down
4 changes: 2 additions & 2 deletions glib/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit d1fdc2e

Please sign in to comment.