@@ -315,18 +315,32 @@ impl<T: GodotClass> Gd<T> {
315315 }
316316
317317 /// 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+
319324 // 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 ) )
324336 }
325337
326338 #[ cfg( feature = "trace" ) ] // itest only.
327339 #[ doc( hidden) ]
328340 pub fn test_refcount ( & self ) -> Option < usize > {
329341 self . maybe_refcount ( )
342+ . transpose ( )
343+ . expect ( "failed to obtain refcount" )
330344 }
331345
332346 /// **Upcast:** convert into a smart pointer to a base class. Always succeeds.
@@ -359,11 +373,20 @@ impl<T: GodotClass> Gd<T> {
359373 . expect ( "Upcast to Object failed. This is a bug; please report it." )
360374 }
361375
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+
362381 /// Equivalent to [`upcast_mut::<Object>()`][Self::upcast_mut], but without bounds.
363382 pub ( crate ) fn upcast_object_mut ( & mut self ) -> & mut classes:: Object {
364383 self . raw . as_object_mut ( )
365384 }
366385
386+ // pub(crate) fn upcast_object_mut_from_ref(&self) -> &mut classes::Object {
387+ // self.raw.as_object_mut()
388+ // }
389+
367390 /// **Upcast shared-ref:** access this object as a shared reference to a base class.
368391 ///
369392 /// 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> {
554577 pub ( crate ) unsafe fn from_obj_sys_or_none (
555578 ptr : sys:: GDExtensionObjectPtr ,
556579 ) -> 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)
558584 }
559585
560586 /// Initializes this `Gd<T>` from the object pointer as a **strong ref**, meaning
0 commit comments