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 committed Oct 11, 2023
1 parent 70f9d7d commit 9174713
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
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
43 changes: 27 additions & 16 deletions glib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3232,12 +3232,34 @@ 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;

// rustdoc-stripper-ignore-next
/// Get the type of the property `property_name` of this object class.
///
/// This returns `None` if the property does not exist.
#[doc(alias = "get_property_type")]
fn property_type(&self, property_name: &str) -> Option<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")]
fn find_property(&self, property_name: &str) -> Option<crate::ParamSpec>;

// rustdoc-stripper-ignore-next
/// Return all [`ParamSpec`](crate::ParamSpec) of the properties of this object class.
#[doc(alias = "g_object_class_list_properties")]
fn list_properties(&self) -> PtrSlice<crate::ParamSpec>;
}

unsafe impl<T: ObjectType + IsClass> ObjectClassExt for Class<T> {
fn has_property(&self, property_name: &str, type_: Option<Type>) -> bool {
let ptype = self.property_type(property_name);

match (ptype, type_) {
Expand All @@ -3247,20 +3269,12 @@ impl ObjectClass {
}
}

// rustdoc-stripper-ignore-next
/// Get the type of the property `property_name` of this object class.
///
/// 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 @@ -3273,10 +3287,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 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 9174713

Please sign in to comment.