Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glib: Implement object class methods via a trait instead of directly … #1203

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
sdroege marked this conversation as resolved.
Show resolved Hide resolved
/// 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,
};
Loading