Skip to content

Commit 2d84535

Browse files
committed
Debug for Gd includes refcount, if available
1 parent 6e291fb commit 2d84535

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

godot-core/src/classes/class_runtime.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ pub(crate) fn debug_string<T: GodotClass>(
2323
) -> std::fmt::Result {
2424
if let Some(id) = obj.instance_id_or_none() {
2525
let class: StringName = obj.dynamic_class_string();
26-
f.debug_struct(ty)
27-
.field("id", &id)
28-
.field("class", &class)
29-
.finish()
26+
27+
let mut builder = f.debug_struct(ty);
28+
builder.field("id", &id).field("class", &class);
29+
if let Some(refcount) = obj.maybe_refcount() {
30+
builder.field("refc", &refcount);
31+
}
32+
builder.finish()
3033
} else {
3134
write!(f, "{ty} {{ freed obj }}")
3235
}
@@ -57,11 +60,16 @@ pub(crate) fn debug_string_with_trait<T: GodotClass>(
5760
) -> std::fmt::Result {
5861
if let Some(id) = obj.instance_id_or_none() {
5962
let class: StringName = obj.dynamic_class_string();
60-
f.debug_struct(ty)
63+
64+
let mut builder = f.debug_struct(ty);
65+
builder
6166
.field("id", &id)
6267
.field("class", &class)
63-
.field("trait", &trt)
64-
.finish()
68+
.field("trait", &trt);
69+
if let Some(refcount) = obj.maybe_refcount() {
70+
builder.field("refc", &refcount);
71+
}
72+
builder.finish()
6573
} else {
6674
write!(f, "{ty} {{ freed obj }}")
6775
}

godot-core/src/obj/gd.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,15 @@ impl<T: GodotClass> Gd<T> {
302302
}
303303
}
304304

305+
/// Returns the reference count, if the dynamic object inherits `RefCounted`; and `None` otherwise.
306+
pub(crate) fn maybe_refcount(&self) -> Option<usize> {
307+
// Fast check if ref-counted without downcast.
308+
self.instance_id_unchecked().is_ref_counted().then(|| {
309+
let rc = self.raw.with_ref_counted(|refc| refc.get_reference_count());
310+
rc as usize
311+
})
312+
}
313+
305314
/// **Upcast:** convert into a smart pointer to a base class. Always succeeds.
306315
///
307316
/// Moves out of this value. If you want to create _another_ smart pointer instance,

0 commit comments

Comments
 (0)