@@ -320,9 +320,21 @@ pub mod mem {
320
320
#[ doc( hidden) ]
321
321
fn maybe_inc_ref < T : GodotClass > ( obj : & Gd < T > ) ;
322
322
323
- /// If ref-counted, then decrement count
323
+ /// If ref-counted, then decrement count. Returns `true` if the count hit 0 and the object can be
324
+ /// safely freed.
325
+ ///
326
+ /// This behavior can be overriden by a script, making it possible for the function to return `false`
327
+ /// even when the reference count hits 0. This is meant to be used to have a separate reference count
328
+ /// from Godot's internal reference count, or otherwise stop the object from being freed when the
329
+ /// reference count hits 0.
330
+ ///
331
+ /// # Safety
332
+ ///
333
+ /// If this method is used on a [`Gd`] that inherits from [`RefCounted`](crate::engine::RefCounted)
334
+ /// then the reference count must either be incremented before it hits 0, or some [`Gd`] referencing
335
+ /// this object must be forgotten.
324
336
#[ doc( hidden) ]
325
- fn maybe_dec_ref < T : GodotClass > ( obj : & Gd < T > ) -> bool ;
337
+ unsafe fn maybe_dec_ref < T : GodotClass > ( obj : & Gd < T > ) -> bool ;
326
338
327
339
/// Check if ref-counted, return `None` if information is not available (dynamic and obj dead)
328
340
#[ doc( hidden) ]
@@ -362,7 +374,7 @@ pub mod mem {
362
374
} ) ;
363
375
}
364
376
365
- fn maybe_dec_ref < T : GodotClass > ( obj : & Gd < T > ) -> bool {
377
+ unsafe fn maybe_dec_ref < T : GodotClass > ( obj : & Gd < T > ) -> bool {
366
378
out ! ( " Stat::dec <{}>" , std:: any:: type_name:: <T >( ) ) ;
367
379
obj. as_ref_counted ( |refc| {
368
380
let is_last = refc. unreference ( ) ;
@@ -407,7 +419,7 @@ pub mod mem {
407
419
}
408
420
}
409
421
410
- fn maybe_dec_ref < T : GodotClass > ( obj : & Gd < T > ) -> bool {
422
+ unsafe fn maybe_dec_ref < T : GodotClass > ( obj : & Gd < T > ) -> bool {
411
423
out ! ( " Dyn::dec <{}>" , std:: any:: type_name:: <T >( ) ) ;
412
424
if obj
413
425
. instance_id_or_none ( )
@@ -435,7 +447,7 @@ pub mod mem {
435
447
impl Memory for ManualMemory {
436
448
fn maybe_init_ref < T : GodotClass > ( _obj : & Gd < T > ) { }
437
449
fn maybe_inc_ref < T : GodotClass > ( _obj : & Gd < T > ) { }
438
- fn maybe_dec_ref < T : GodotClass > ( _obj : & Gd < T > ) -> bool {
450
+ unsafe fn maybe_dec_ref < T : GodotClass > ( _obj : & Gd < T > ) -> bool {
439
451
false
440
452
}
441
453
fn is_ref_counted < T : GodotClass > ( _obj : & Gd < T > ) -> Option < bool > {
0 commit comments