File tree Expand file tree Collapse file tree 6 files changed +23
-7
lines changed Expand file tree Collapse file tree 6 files changed +23
-7
lines changed Original file line number Diff line number Diff line change @@ -7,9 +7,13 @@ use std::fmt;
7
7
use std:: mem;
8
8
9
9
impl Event {
10
+ pub fn is < T : EventKind > ( & self ) -> bool {
11
+ T :: event_types ( ) . contains ( & self . event_type ( ) )
12
+ }
13
+
10
14
pub fn downcast < T : EventKind > ( self ) -> Result < T , Event > {
11
15
unsafe {
12
- if T :: event_types ( ) . contains ( & self . event_type ( ) ) {
16
+ if self . is :: < T > ( ) {
13
17
Ok ( from_glib_full ( self . to_glib_full ( ) ) )
14
18
} else {
15
19
Err ( self )
@@ -19,7 +23,7 @@ impl Event {
19
23
20
24
pub fn downcast_ref < T : EventKind > ( & self ) -> Option < & T > {
21
25
unsafe {
22
- if T :: event_types ( ) . contains ( & self . event_type ( ) ) {
26
+ if self . is :: < T > ( ) {
23
27
Some ( & * ( self as * const Event as * const T ) )
24
28
} else {
25
29
None
Original file line number Diff line number Diff line change @@ -5,6 +5,10 @@ use glib::translate::*;
5
5
use glib:: StaticType ;
6
6
7
7
impl RenderNode {
8
+ pub fn is < T : IsRenderNode > ( & self ) -> bool {
9
+ T :: NODE_TYPE == self . node_type ( )
10
+ }
11
+
8
12
#[ doc( alias = "gsk_render_node_deserialize" ) ]
9
13
pub fn deserialize ( bytes : & glib:: Bytes ) -> Option < Self > {
10
14
assert_initialized_main_thread ! ( ) ;
@@ -51,7 +55,7 @@ impl RenderNode {
51
55
52
56
pub fn downcast < T : IsRenderNode > ( self ) -> Result < T , Self > {
53
57
unsafe {
54
- if self . node_type ( ) == T :: NODE_TYPE {
58
+ if self . is :: < T > ( ) {
55
59
Ok ( from_glib_full ( self . to_glib_full ( ) ) )
56
60
} else {
57
61
Err ( self )
@@ -61,7 +65,7 @@ impl RenderNode {
61
65
62
66
pub fn downcast_ref < T : IsRenderNode > ( & self ) -> Option < & T > {
63
67
unsafe {
64
- if self . node_type ( ) == T :: NODE_TYPE {
68
+ if self . is :: < T > ( ) {
65
69
Some ( & * ( self as * const RenderNode as * const T ) )
66
70
} else {
67
71
None
Original file line number Diff line number Diff line change @@ -57,5 +57,7 @@ mod tests {
57
57
let expr2 = ConstantExpression :: for_value ( & "hello" . to_value ( ) ) ;
58
58
assert_eq ! ( expr2. value( ) . get:: <String >( ) . unwrap( ) , "hello" ) ;
59
59
assert_eq ! ( expr2. value_as:: <String >( ) , "hello" ) ;
60
+
61
+ assert ! ( expr1. is:: <ConstantExpression >( ) ) ;
60
62
}
61
63
}
Original file line number Diff line number Diff line change @@ -23,9 +23,13 @@ pub unsafe trait IsExpression:
23
23
}
24
24
25
25
impl Expression {
26
+ pub fn is < E : IsExpression > ( & self ) -> bool {
27
+ self . type_ ( ) . is_a ( E :: static_type ( ) )
28
+ }
29
+
26
30
pub fn downcast < E : IsExpression > ( self ) -> Result < E , Expression > {
27
31
unsafe {
28
- if self . type_ ( ) == E :: static_type ( ) {
32
+ if self . is :: < E > ( ) {
29
33
Ok ( from_glib_full ( self . to_glib_full ( ) ) )
30
34
} else {
31
35
Err ( self )
@@ -35,7 +39,7 @@ impl Expression {
35
39
36
40
pub fn downcast_ref < E : IsExpression > ( & self ) -> Option < & E > {
37
41
unsafe {
38
- if self . type_ ( ) == E :: static_type ( ) {
42
+ if self . is :: < E > ( ) {
39
43
Some ( & * ( self as * const Expression as * const E ) )
40
44
} else {
41
45
None
Original file line number Diff line number Diff line change @@ -25,5 +25,6 @@ mod tests {
25
25
let obj = crate :: IconTheme :: new ( ) ;
26
26
let expr = ObjectExpression :: new ( & obj) ;
27
27
assert_eq ! ( expr. object( ) . unwrap( ) , obj) ;
28
+ assert ! ( expr. is:: <ObjectExpression >( ) ) ;
28
29
}
29
30
}
Original file line number Diff line number Diff line change @@ -24,10 +24,11 @@ mod tests {
24
24
25
25
#[ test]
26
26
fn test_property_expression ( ) {
27
- let _prop_expr = PropertyExpression :: new (
27
+ let prop_expr = PropertyExpression :: new (
28
28
crate :: StringObject :: static_type ( ) ,
29
29
crate :: Expression :: NONE ,
30
30
"string" ,
31
31
) ;
32
+ assert ! ( prop_expr. is:: <PropertyExpression >( ) ) ;
32
33
}
33
34
}
You can’t perform that action at this time.
0 commit comments