Skip to content

Commit 671fcb5

Browse files
authored
Merge pull request #901 from godot-rust/qol/deprecate-instance-utilities
Deprecate instance utilities in `godot::global`
2 parents e47936e + ece7360 commit 671fcb5

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

godot-core/src/global/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,30 @@ pub use crate::gen::central::global_enums::*;
3131
pub use crate::gen::utilities::*;
3232

3333
// This is needed for generated classes to find symbols, even those that have been moved to crate::builtin.
34+
use crate::builtin::Variant;
3435
#[allow(unused_imports)] // micromanaging imports for generated code is not fun
3536
pub(crate) use crate::builtin::{Corner, EulerOrder, Side};
37+
use crate::obj::Gd;
38+
39+
// ----------------------------------------------------------------------------------------------------------------------------------------------
40+
// Deprecations
41+
42+
// Reminder: remove #![allow(deprecated)] in utilities.test along with the below functions.
43+
44+
#[deprecated = "Instance utilities in `godot::global` will be removed. Use methods on `Gd` and `InstanceId` instead.\n\
45+
For detailed reasons, see https://github.com/godot-rust/gdext/pull/892."]
46+
pub fn instance_from_id(instance_id: i64) -> Option<Gd<crate::classes::Object>> {
47+
crate::gen::utilities::instance_from_id(instance_id)
48+
}
49+
50+
#[deprecated = "Instance utilities in `godot::global` will be removed. Use methods on `Gd` and `InstanceId` instead.\n\
51+
For detailed reasons, see https://github.com/godot-rust/gdext/pull/892."]
52+
pub fn is_instance_valid(instance: Variant) -> bool {
53+
crate::gen::utilities::is_instance_valid(instance)
54+
}
55+
56+
#[deprecated = "Instance utilities in `godot::global` will be removed. Use methods on `Gd` and `InstanceId` instead.\n\
57+
For detailed reasons, see https://github.com/godot-rust/gdext/pull/892."]
58+
pub fn is_instance_id_valid(instance_id: i64) -> bool {
59+
crate::gen::utilities::is_instance_id_valid(instance_id)
60+
}

godot-core/src/obj/gd.rs

+3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,12 @@ impl<T: GodotClass> Gd<T> {
209209

210210
/// ⚠️ Looks up the given instance ID and returns the associated object.
211211
///
212+
/// Corresponds to Godot's global function `instance_from_id()`.
213+
///
212214
/// # Panics
213215
/// If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
214216
/// is not compatible with `T`.
217+
#[doc(alias = "instance_from_id")]
215218
pub fn from_instance_id(instance_id: InstanceId) -> Self {
216219
Self::try_from_instance_id(instance_id).unwrap_or_else(|err| {
217220
panic!(

godot-core/src/obj/instance_id.rs

+11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ impl InstanceId {
5858
self.to_u64() & (1u64 << 63) != 0
5959
}
6060

61+
/// Dynamically checks if the instance behind the ID exists.
62+
///
63+
/// Rather slow, involves engine round-trip plus object DB lookup. If you need the object, use
64+
/// [`Gd::from_instance_id()`][crate::obj::Gd::from_instance_id] instead.
65+
///
66+
/// This corresponds to Godot's global function `is_instance_id_valid()`.
67+
#[doc(alias = "is_instance_id_valid")]
68+
pub fn lookup_validity(self) -> bool {
69+
crate::gen::utilities::is_instance_id_valid(self.to_i64())
70+
}
71+
6172
// Private: see rationale above
6273
pub(crate) fn to_u64(self) -> u64 {
6374
self.value.get()

godot-core/src/obj/raw_gd.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::obj::bounds::{Declarer, DynMemory as _};
1919
use crate::obj::rtti::ObjectRtti;
2020
use crate::obj::{bounds, Bounds, GdDerefTarget, GdMut, GdRef, GodotClass, InstanceId};
2121
use crate::storage::{InstanceCache, InstanceStorage, Storage};
22-
use crate::{classes, global, out};
22+
use crate::{classes, out};
2323

2424
/// Low-level bindings for object pointers in Godot.
2525
///
@@ -112,7 +112,7 @@ impl<T: GodotClass> RawGd<T> {
112112
pub(crate) fn is_instance_valid(&self) -> bool {
113113
self.cached_rtti
114114
.as_ref()
115-
.map(|rtti| global::is_instance_id_valid(rtti.instance_id().to_i64()))
115+
.map(|rtti| rtti.instance_id().lookup_validity())
116116
.unwrap_or(false)
117117
}
118118

itest/rust/src/engine_tests/utilities_test.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8+
// TODO remove once instance_from_id() etc are removed.
9+
#![allow(deprecated)]
10+
811
use crate::framework::itest;
912

1013
use godot::builtin::{GString, Variant};

itest/rust/src/object_tests/object_test.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use godot::classes::{
1313
file_access, Area2D, Camera3D, Engine, FileAccess, IRefCounted, Node, Node3D, Object,
1414
RefCounted,
1515
};
16+
#[allow(deprecated)]
1617
use godot::global::instance_from_id;
1718
use godot::meta::{FromGodot, GodotType, ToGodot};
1819
use godot::obj::{Base, Gd, Inherits, InstanceId, NewAlloc, NewGd, RawGd};
@@ -171,6 +172,7 @@ fn object_instance_from_id() {
171172

172173
let instance_id = node.instance_id();
173174

175+
#[allow(deprecated)]
174176
let gd_from_instance_id = instance_from_id(instance_id.to_i64())
175177
.expect("instance should be valid")
176178
.cast::<Node>();
@@ -182,6 +184,7 @@ fn object_instance_from_id() {
182184

183185
#[itest]
184186
fn object_instance_from_invalid_id() {
187+
#[allow(deprecated)]
185188
let gd_from_instance_id = instance_from_id(0);
186189

187190
assert!(

0 commit comments

Comments
 (0)