4
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
5
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
6
*/
7
-
8
7
use crate :: framework:: { expect_panic, itest} ;
9
8
// Test that all important dyn-related symbols are in the prelude.
10
9
use godot:: prelude:: * ;
11
10
12
11
#[ itest]
13
12
fn dyn_gd_creation_bind ( ) {
14
13
// Type inference on this works because only 1 AsDyn<...> trait is implemented for RefcHealth. It would fail with another.
15
- let _unused = DynGd :: from_gd ( Gd :: from_object ( RefcHealth { hp : 1 } ) ) ;
14
+ let _unused = Gd :: from_object ( RefcHealth { hp : 1 } ) . into_dyn ( ) ;
16
15
17
16
let user_obj = RefcHealth { hp : 34 } ;
18
- let mut dyn_gd = DynGd :: from_gd ( Gd :: from_object ( user_obj) ) ;
17
+ let mut dyn_gd = Gd :: from_object ( user_obj) . into_dyn ( ) ;
19
18
20
19
{
21
20
// Exclusive bind.
22
21
// Interesting: this can be type inferred because RefcHealth implements only 1 AsDyn<...> trait.
23
22
// If there were another, type inference would fail.
24
- let mut health = dyn_gd. dbind_mut ( ) ;
23
+ let mut health = dyn_gd. dyn_bind_mut ( ) ;
25
24
health. deal_damage ( 4 ) ;
26
25
}
27
26
{
28
27
// Test multiple shared binds.
29
- let health_a = dyn_gd. dbind ( ) ;
30
- let health_b = dyn_gd. dbind ( ) ;
28
+ let health_a = dyn_gd. dyn_bind ( ) ;
29
+ let health_b = dyn_gd. dyn_bind ( ) ;
31
30
32
31
assert_eq ! ( health_b. get_hitpoints( ) , 30 ) ;
33
32
assert_eq ! ( health_a. get_hitpoints( ) , 30 ) ;
34
33
}
35
34
{
36
- let mut health = dyn_gd. dbind_mut ( ) ;
35
+ let mut health = dyn_gd. dyn_bind_mut ( ) ;
37
36
health. kill ( ) ;
38
37
39
38
assert_eq ! ( health. get_hitpoints( ) , 0 ) ;
@@ -45,15 +44,13 @@ fn dyn_gd_creation_deref() {
45
44
let node = foreign:: NodeHealth :: new_alloc ( ) ;
46
45
let original_id = node. instance_id ( ) ;
47
46
48
- // let mut node = node.into_dyn::<dyn Health>();
49
- // The first line only works because both type parameters are inferred as RefcHealth, and there's no `dyn Health`.
50
- let mut node = DynGd :: from_gd ( node) ;
47
+ let mut node = node. into_dyn :: < dyn Health > ( ) ;
51
48
52
49
let dyn_id = node. instance_id ( ) ;
53
50
assert_eq ! ( dyn_id, original_id) ;
54
51
55
- deal_20_damage ( & mut * node. dbind_mut ( ) ) ;
56
- assert_eq ! ( node. dbind ( ) . get_hitpoints( ) , 80 ) ;
52
+ deal_20_damage ( & mut * node. dyn_bind_mut ( ) ) ;
53
+ assert_eq ! ( node. dyn_bind ( ) . get_hitpoints( ) , 80 ) ;
57
54
58
55
node. free ( ) ;
59
56
}
@@ -72,40 +69,40 @@ fn dyn_gd_upcast() {
72
69
let mut node = concrete. clone ( ) . upcast :: < Node > ( ) ;
73
70
let object = concrete. upcast :: < Object > ( ) ;
74
71
75
- node. dbind_mut ( ) . deal_damage ( 25 ) ;
72
+ node. dyn_bind_mut ( ) . deal_damage ( 25 ) ;
76
73
77
74
// Make sure identity is intact.
78
75
assert_eq ! ( node. instance_id( ) , original_copy. instance_id( ) ) ;
79
76
80
- // Ensure applied to the object polymorphically. Concrete object can access bind(), no dbind ().
77
+ // Ensure applied to the object polymorphically. Concrete object can access bind(), no dyn_bind ().
81
78
assert_eq ! ( original_copy. bind( ) . get_hitpoints( ) , 75 ) ;
82
79
83
- // Check also another angle (via Object). Here dbind ().
84
- assert_eq ! ( object. dbind ( ) . get_hitpoints( ) , 75 ) ;
80
+ // Check also another angle (via Object). Here dyn_bind ().
81
+ assert_eq ! ( object. dyn_bind ( ) . get_hitpoints( ) , 75 ) ;
85
82
86
83
node. free ( ) ;
87
84
}
88
85
89
86
#[ itest]
90
87
fn dyn_gd_exclusive_guard ( ) {
91
- let mut a = DynGd :: from_gd ( foreign:: NodeHealth :: new_alloc ( ) ) ;
88
+ let mut a = foreign:: NodeHealth :: new_alloc ( ) . into_dyn ( ) ;
92
89
let mut b = a. clone ( ) ;
93
90
94
- let guard = a. dbind_mut ( ) ;
91
+ let guard = a. dyn_bind_mut ( ) ;
95
92
96
93
expect_panic (
97
- "Cannot acquire dbind () guard while dbind_mut () is held" ,
94
+ "Cannot acquire dyn_bind () guard while dyn_bind_mut () is held" ,
98
95
|| {
99
- let _ = b. dbind ( ) ;
96
+ let _ = b. dyn_bind ( ) ;
100
97
} ,
101
98
) ;
102
99
expect_panic (
103
- "Cannot acquire 2nd dbind_mut () guard while dbind_mut () is held" ,
100
+ "Cannot acquire 2nd dyn_bind_mut () guard while dyn_bind_mut () is held" ,
104
101
|| {
105
- let _ = b. dbind_mut ( ) ;
102
+ let _ = b. dyn_bind_mut ( ) ;
106
103
} ,
107
104
) ;
108
- expect_panic ( "Cannot free object while dbind_mut () is held" , || {
105
+ expect_panic ( "Cannot free object while dyn_bind_mut () is held" , || {
109
106
b. free ( ) ;
110
107
} ) ;
111
108
@@ -115,26 +112,26 @@ fn dyn_gd_exclusive_guard() {
115
112
116
113
#[ itest]
117
114
fn dyn_gd_shared_guard ( ) {
118
- let a = DynGd :: from_gd ( foreign:: NodeHealth :: new_alloc ( ) ) ;
115
+ let a = foreign:: NodeHealth :: new_alloc ( ) . into_dyn ( ) ;
119
116
let b = a. clone ( ) ;
120
117
let mut c = a. clone ( ) ;
121
118
122
- let guard_a = a. dbind ( ) ;
119
+ let guard_a = a. dyn_bind ( ) ;
123
120
124
- // CAN acquire another dbind () while an existing one exists.
125
- let guard_b = b. dbind ( ) ;
121
+ // CAN acquire another dyn_bind () while an existing one exists.
122
+ let guard_b = b. dyn_bind ( ) ;
126
123
drop ( guard_a) ;
127
124
128
125
// guard_b still alive here.
129
126
expect_panic (
130
- "Cannot acquire dbind_mut () guard while dbind () is held" ,
127
+ "Cannot acquire dyn_bind_mut () guard while dyn_bind () is held" ,
131
128
|| {
132
- let _ = c. dbind_mut ( ) ;
129
+ let _ = c. dyn_bind_mut ( ) ;
133
130
} ,
134
131
) ;
135
132
136
133
// guard_b still alive here.
137
- expect_panic ( "Cannot free object while dbind () is held" , || {
134
+ expect_panic ( "Cannot free object while dyn_bind () is held" , || {
138
135
c. free ( ) ;
139
136
} ) ;
140
137
@@ -153,6 +150,16 @@ fn dyn_gd_downgrade() {
153
150
assert_eq ! ( gd. instance_id( ) , dyn_id) ;
154
151
}
155
152
153
+ #[ itest]
154
+ fn dyn_gd_call_godot_method ( ) {
155
+ let mut node = foreign:: NodeHealth :: new_alloc ( ) . into_dyn ( ) ;
156
+
157
+ node. set_name ( "dyn-name!" ) ;
158
+ assert_eq ! ( node. get_name( ) , "dyn-name!" . into( ) ) ;
159
+
160
+ node. free ( ) ;
161
+ }
162
+
156
163
#[ itest]
157
164
fn dyn_gd_pass_to_godot_api ( ) {
158
165
let child = foreign:: NodeHealth :: new_alloc ( ) . into_dyn ( ) ;
0 commit comments