@@ -315,18 +315,32 @@ impl<T: GodotClass> Gd<T> {
315
315
}
316
316
317
317
/// Returns the reference count, if the dynamic object inherits `RefCounted`; and `None` otherwise.
318
- pub ( crate ) fn maybe_refcount ( & self ) -> Option < usize > {
318
+ ///
319
+ /// Returns `Err(())` if obtaining reference count failed, due to being called during init/drop.
320
+ pub ( crate ) fn maybe_refcount ( & self ) -> Option < Result < usize , ( ) > > {
321
+ // May become infallible if implemented via call() on Object, if ref-count bit of instance ID is set.
322
+ // This would likely be more efficient, too.
323
+
319
324
// Fast check if ref-counted without downcast.
320
- self . instance_id ( ) . is_ref_counted ( ) . then ( || {
321
- let rc = self . raw . with_ref_counted ( |refc| refc. get_reference_count ( ) ) ;
322
- rc as usize
323
- } )
325
+ if !self . instance_id ( ) . is_ref_counted ( ) {
326
+ return None ;
327
+ }
328
+
329
+ // Optimization: call `get_reference_count()` directly. Might also increase reliability and obviate the need for Result.
330
+
331
+ let rc = self
332
+ . raw
333
+ . try_with_ref_counted ( |refc| refc. get_reference_count ( ) ) ;
334
+
335
+ Some ( rc. map ( |i| i as usize ) )
324
336
}
325
337
326
338
#[ cfg( feature = "trace" ) ] // itest only.
327
339
#[ doc( hidden) ]
328
340
pub fn test_refcount ( & self ) -> Option < usize > {
329
341
self . maybe_refcount ( )
342
+ . transpose ( )
343
+ . expect ( "failed to obtain refcount" )
330
344
}
331
345
332
346
/// **Upcast:** convert into a smart pointer to a base class. Always succeeds.
@@ -359,11 +373,20 @@ impl<T: GodotClass> Gd<T> {
359
373
. expect ( "Upcast to Object failed. This is a bug; please report it." )
360
374
}
361
375
376
+ // /// Equivalent to [`upcast_mut::<Object>()`][Self::upcast_mut], but without bounds.
377
+ // pub(crate) fn upcast_object_ref(&self) -> &classes::Object {
378
+ // self.raw.as_object_ref()
379
+ // }
380
+
362
381
/// Equivalent to [`upcast_mut::<Object>()`][Self::upcast_mut], but without bounds.
363
382
pub ( crate ) fn upcast_object_mut ( & mut self ) -> & mut classes:: Object {
364
383
self . raw . as_object_mut ( )
365
384
}
366
385
386
+ // pub(crate) fn upcast_object_mut_from_ref(&self) -> &mut classes::Object {
387
+ // self.raw.as_object_mut()
388
+ // }
389
+
367
390
/// **Upcast shared-ref:** access this object as a shared reference to a base class.
368
391
///
369
392
/// This is semantically equivalent to multiple applications of [`Self::deref()`]. Not really useful on its own, but combined with
@@ -554,7 +577,10 @@ impl<T: GodotClass> Gd<T> {
554
577
pub ( crate ) unsafe fn from_obj_sys_or_none (
555
578
ptr : sys:: GDExtensionObjectPtr ,
556
579
) -> Result < Self , ConvertError > {
557
- Self :: try_from_ffi ( RawGd :: from_obj_sys ( ptr) )
580
+ // Used to have a flag to select RawGd::from_obj_sys_weak(ptr) for Base::to_init_gd(), but solved differently in the end.
581
+ let obj = RawGd :: from_obj_sys ( ptr) ;
582
+
583
+ Self :: try_from_ffi ( obj)
558
584
}
559
585
560
586
/// Initializes this `Gd<T>` from the object pointer as a **strong ref**, meaning
0 commit comments