@@ -151,6 +151,36 @@ pub unsafe trait GodotObject: Sized + crate::private::godot_object::Sealed {
151
151
{
152
152
Ref :: from_sys ( self . as_raw ( ) . sys ( ) )
153
153
}
154
+
155
+ /// Recovers a instance ID previously returned by `Object::get_instance_id` if the object is
156
+ /// still alive. See also `TRef::try_from_instance_id`.
157
+ ///
158
+ /// # Safety
159
+ ///
160
+ /// During the entirety of `'a`, the thread from which `try_from_instance_id` is called must
161
+ /// have exclusive access to the underlying object, if it is still alive.
162
+ #[ inline]
163
+ unsafe fn try_from_instance_id < ' a > ( id : i64 ) -> Option < TRef < ' a , Self , Shared > > {
164
+ TRef :: try_from_instance_id ( id)
165
+ }
166
+
167
+ /// Recovers a instance ID previously returned by `Object::get_instance_id` if the object is
168
+ /// still alive, and panics otherwise. This does **NOT** guarantee that the resulting
169
+ /// reference is safe to use.
170
+ ///
171
+ /// # Panics
172
+ ///
173
+ /// Panics if the given id refers to a destroyed object. For a non-panicking version, see
174
+ /// `try_from_instance_id`.
175
+ ///
176
+ /// # Safety
177
+ ///
178
+ /// During the entirety of `'a`, the thread from which `try_from_instance_id` is called must
179
+ /// have exclusive access to the underlying object, if it is still alive.
180
+ #[ inline]
181
+ unsafe fn from_instance_id < ' a > ( id : i64 ) -> TRef < ' a , Self , Shared > {
182
+ TRef :: from_instance_id ( id)
183
+ }
154
184
}
155
185
156
186
/// Marker trait for API types that are subclasses of another type. This trait is implemented
@@ -892,6 +922,41 @@ where
892
922
}
893
923
}
894
924
925
+ impl < ' a , T : GodotObject > TRef < ' a , T , Shared > {
926
+ /// Recovers a instance ID previously returned by `Object::get_instance_id` if the object is
927
+ /// still alive.
928
+ ///
929
+ /// # Safety
930
+ ///
931
+ /// During the entirety of `'a`, the thread from which `try_from_instance_id` is called must
932
+ /// have exclusive access to the underlying object, if it is still alive.
933
+ #[ inline]
934
+ pub unsafe fn try_from_instance_id ( id : i64 ) -> Option < Self > {
935
+ let api = get_api ( ) ;
936
+ let ptr = NonNull :: new ( ( api. godot_instance_from_id ) ( id as sys:: godot_int ) ) ?;
937
+ let raw = RawObject :: try_from_sys_ref ( ptr) ?;
938
+ Some ( TRef :: new ( T :: cast_ref ( raw) ) )
939
+ }
940
+
941
+ /// Recovers a instance ID previously returned by `Object::get_instance_id` if the object is
942
+ /// still alive, and panics otherwise. This does **NOT** guarantee that the resulting
943
+ /// reference is safe to use.
944
+ ///
945
+ /// # Panics
946
+ ///
947
+ /// Panics if the given id refers to a destroyed object. For a non-panicking version, see
948
+ /// `try_from_instance_id`.
949
+ ///
950
+ /// # Safety
951
+ ///
952
+ /// During the entirety of `'a`, the thread from which `try_from_instance_id` is called must
953
+ /// have exclusive access to the underlying object, if it is still alive.
954
+ #[ inline]
955
+ pub unsafe fn from_instance_id ( id : i64 ) -> Self {
956
+ Self :: try_from_instance_id ( id) . expect ( "instance should be alive" )
957
+ }
958
+ }
959
+
895
960
/// Trait for safe conversion from Godot object references into API method arguments. This is
896
961
/// a sealed trait with no public interface.
897
962
///
0 commit comments