@@ -171,6 +171,36 @@ fn base_swapping() {
171
171
two. free ( ) ;
172
172
}
173
173
174
+ #[ itest]
175
+ fn base_refcounted_weak_reference ( ) {
176
+ // Not new_gd(), to not interfere with to_init_gd() ref-count handling.
177
+ let obj = RefcBased :: create_one ( ) ;
178
+
179
+ let initial_refcount = obj. get_reference_count ( ) ;
180
+ assert_eq ! ( initial_refcount, 1 ) ;
181
+
182
+ {
183
+ let bind_guard = obj. bind ( ) ;
184
+ let base_guard = bind_guard. base ( ) ;
185
+
186
+ let intermediate_refcount = obj. get_reference_count ( ) ;
187
+ assert_eq ! (
188
+ intermediate_refcount, 1 ,
189
+ "base() should not increment refcount"
190
+ ) ;
191
+
192
+ // Call an API to ensure Base is functional.
193
+ let class_name = base_guard. get_class ( ) ;
194
+ assert_eq ! ( class_name, "RefcBased" . into( ) ) ;
195
+ }
196
+
197
+ let final_refcount = obj. get_reference_count ( ) ;
198
+ assert_eq ! (
199
+ final_refcount, 1 ,
200
+ "refcount should remain unchanged after dropping base guard"
201
+ ) ;
202
+ }
203
+
174
204
fn create_object_with_extracted_base ( ) -> ( Gd < Baseless > , Base < Node2D > ) {
175
205
let mut extracted_base = None ;
176
206
let obj = Baseless :: smuggle_out ( & mut extracted_base) ;
@@ -267,6 +297,11 @@ impl IRefCounted for RefcBased {
267
297
// Only needed in base_init_test.rs.
268
298
#[ godot_api( no_typed_signals) ]
269
299
impl RefcBased {
300
+ /// No `to_init_gd()` call, so the reference count is 1 after initialization.
301
+ pub fn create_one ( ) -> Gd < Self > {
302
+ Gd :: from_init_fn ( |base| Self { base } )
303
+ }
304
+
270
305
/// Used in `base_init_test.rs` to test that a base pointer can be extracted during initialization.
271
306
pub fn split_simple ( ) -> ( Gd < Self > , Gd < RefCounted > ) {
272
307
let mut moved_out = None ;
0 commit comments