Skip to content

Commit

Permalink
glib: Remove type parameter from Object::has_property() and add sep…
Browse files Browse the repository at this point in the history
…arate `has_property_with_type()`

Most of the time the property type is not interesting and the `None`
makes the code harder to understand. Having a separate, more descriptive
function for also checking the type seems better.
  • Loading branch information
sdroege committed Nov 6, 2024
1 parent cc97e01 commit 41daf6c
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions glib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1750,11 +1750,13 @@ pub trait ObjectExt: ObjectType {
#[doc(alias = "g_object_get_property")]
fn property_value(&self, property_name: &str) -> Value;

// rustdoc-stripper-ignore-next
/// Check if the object has a property `property_name`.
fn has_property(&self, property_name: &str) -> bool;

// rustdoc-stripper-ignore-next
/// Check if the object has a property `property_name` of the given `type_`.
///
/// If no type is provided then only the existence of the property is checked.
fn has_property(&self, property_name: &str, type_: Option<Type>) -> bool;
fn has_property_with_type(&self, property_name: &str, type_: Type) -> bool;

// rustdoc-stripper-ignore-next
/// Get the type of the property `property_name` of this object.
Expand Down Expand Up @@ -2455,8 +2457,13 @@ impl<T: ObjectType> ObjectExt for T {
}
}

fn has_property(&self, property_name: &str, type_: Option<Type>) -> bool {
self.object_class().has_property(property_name, type_)
fn has_property(&self, property_name: &str) -> bool {
self.object_class().has_property(property_name)
}

fn has_property_with_type(&self, property_name: &str, type_: Type) -> bool {
self.object_class()
.has_property_with_type(property_name, type_)
}

fn property_type(&self, property_name: &str) -> Option<Type> {
Expand Down Expand Up @@ -3316,16 +3323,15 @@ fn validate_signal_arguments(type_: Type, signal_query: &SignalQuery, args: &mut
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.
fn has_property(&self, property_name: &str, type_: Option<Type>) -> bool {
let ptype = self.property_type(property_name);
fn has_property(&self, property_name: &str) -> bool {
self.find_property(property_name).is_some()
}

match (ptype, type_) {
(None, _) => false,
(Some(_), None) => true,
(Some(ptype), Some(type_)) => ptype == type_,
}
// rustdoc-stripper-ignore-next
/// Check if the object class has a property `property_name` of the given `type_`.
fn has_property_with_type(&self, property_name: &str, type_: Type) -> bool {
self.property_type(property_name)
.is_some_and(|ptype| ptype == type_)
}

// rustdoc-stripper-ignore-next
Expand Down Expand Up @@ -4262,17 +4268,16 @@ impl<T: IsInterface> Interface<T> {

impl<T: IsA<Object> + IsInterface> Interface<T> {
// rustdoc-stripper-ignore-next
/// Check if this interface 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 {
let ptype = self.property_type(property_name);
/// Check if the interface has a property `property_name` of the given `type_`.
fn has_property(&self, property_name: &str) -> bool {
self.find_property(property_name).is_some()
}

match (ptype, type_) {
(None, _) => false,
(Some(_), None) => true,
(Some(ptype), Some(type_)) => ptype == type_,
}
// rustdoc-stripper-ignore-next
/// Check if the interface has a property `property_name` of the given `type_`.
fn has_property_with_type(&self, property_name: &str, type_: Type) -> bool {
self.property_type(property_name)
.is_some_and(|ptype| ptype == type_)
}

// rustdoc-stripper-ignore-next
Expand Down

0 comments on commit 41daf6c

Please sign in to comment.