Skip to content

Commit 5ee7c97

Browse files
committed
Test base/base_mut refcount semantics
1 parent 8f17fff commit 5ee7c97

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

itest/rust/src/object_tests/base_test.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,36 @@ fn base_swapping() {
171171
two.free();
172172
}
173173

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+
174204
fn create_object_with_extracted_base() -> (Gd<Baseless>, Base<Node2D>) {
175205
let mut extracted_base = None;
176206
let obj = Baseless::smuggle_out(&mut extracted_base);
@@ -267,6 +297,11 @@ impl IRefCounted for RefcBased {
267297
// Only needed in base_init_test.rs.
268298
#[godot_api(no_typed_signals)]
269299
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+
270305
/// Used in `base_init_test.rs` to test that a base pointer can be extracted during initialization.
271306
pub fn split_simple() -> (Gd<Self>, Gd<RefCounted>) {
272307
let mut moved_out = None;

0 commit comments

Comments
 (0)