Skip to content

Commit 561e05d

Browse files
Add is method to fundamental types
Fixes #860
1 parent e58ac1e commit 561e05d

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

gdk4/src/event.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ use std::fmt;
77
use std::mem;
88

99
impl Event {
10+
pub fn is<T: EventKind>(&self) -> bool {
11+
T::event_types().contains(&self.event_type())
12+
}
13+
1014
pub fn downcast<T: EventKind>(self) -> Result<T, Event> {
1115
unsafe {
12-
if T::event_types().contains(&self.event_type()) {
16+
if self.is::<T>() {
1317
Ok(from_glib_full(self.to_glib_full()))
1418
} else {
1519
Err(self)
@@ -19,7 +23,7 @@ impl Event {
1923

2024
pub fn downcast_ref<T: EventKind>(&self) -> Option<&T> {
2125
unsafe {
22-
if T::event_types().contains(&self.event_type()) {
26+
if self.is::<T>() {
2327
Some(&*(self as *const Event as *const T))
2428
} else {
2529
None

gsk4/src/render_node.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use glib::translate::*;
55
use glib::StaticType;
66

77
impl RenderNode {
8+
pub fn is<T: IsRenderNode>(&self) -> bool {
9+
T::NODE_TYPE == self.node_type()
10+
}
11+
812
#[doc(alias = "gsk_render_node_deserialize")]
913
pub fn deserialize(bytes: &glib::Bytes) -> Option<Self> {
1014
assert_initialized_main_thread!();
@@ -51,7 +55,7 @@ impl RenderNode {
5155

5256
pub fn downcast<T: IsRenderNode>(self) -> Result<T, Self> {
5357
unsafe {
54-
if self.node_type() == T::NODE_TYPE {
58+
if self.is::<T>() {
5559
Ok(from_glib_full(self.to_glib_full()))
5660
} else {
5761
Err(self)
@@ -61,7 +65,7 @@ impl RenderNode {
6165

6266
pub fn downcast_ref<T: IsRenderNode>(&self) -> Option<&T> {
6367
unsafe {
64-
if self.node_type() == T::NODE_TYPE {
68+
if self.is::<T>() {
6569
Some(&*(self as *const RenderNode as *const T))
6670
} else {
6771
None

gtk4/src/constant_expression.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,7 @@ mod tests {
5757
let expr2 = ConstantExpression::for_value(&"hello".to_value());
5858
assert_eq!(expr2.value().get::<String>().unwrap(), "hello");
5959
assert_eq!(expr2.value_as::<String>(), "hello");
60+
61+
assert!(expr1.is::<ConstantExpression>());
6062
}
6163
}

gtk4/src/expression.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ pub unsafe trait IsExpression:
2323
}
2424

2525
impl Expression {
26+
pub fn is<E: IsExpression>(&self) -> bool {
27+
self.type_().is_a(E::static_type())
28+
}
29+
2630
pub fn downcast<E: IsExpression>(self) -> Result<E, Expression> {
2731
unsafe {
28-
if self.type_() == E::static_type() {
32+
if self.is::<E>() {
2933
Ok(from_glib_full(self.to_glib_full()))
3034
} else {
3135
Err(self)
@@ -35,7 +39,7 @@ impl Expression {
3539

3640
pub fn downcast_ref<E: IsExpression>(&self) -> Option<&E> {
3741
unsafe {
38-
if self.type_() == E::static_type() {
42+
if self.is::<E>() {
3943
Some(&*(self as *const Expression as *const E))
4044
} else {
4145
None

gtk4/src/object_expression.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ mod tests {
2525
let obj = crate::IconTheme::new();
2626
let expr = ObjectExpression::new(&obj);
2727
assert_eq!(expr.object().unwrap(), obj);
28+
assert!(expr.is::<ObjectExpression>());
2829
}
2930
}

gtk4/src/property_expression.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ mod tests {
2424

2525
#[test]
2626
fn test_property_expression() {
27-
let _prop_expr = PropertyExpression::new(
27+
let prop_expr = PropertyExpression::new(
2828
crate::StringObject::static_type(),
2929
crate::Expression::NONE,
3030
"string",
3131
);
32+
assert!(prop_expr.is::<PropertyExpression>());
3233
}
3334
}

0 commit comments

Comments
 (0)