Skip to content

Commit 7e92288

Browse files
committed
Utilities around instance ID: remove from public API + document
1 parent 404b49b commit 7e92288

File tree

7 files changed

+46
-74
lines changed

7 files changed

+46
-74
lines changed

godot-codegen/src/models/domain_mapping.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ impl UtilityFunction {
546546
if special_cases::is_utility_function_deleted(function, ctx) {
547547
return None;
548548
}
549+
let is_private = special_cases::is_utility_function_private(function);
549550

550551
// Some vararg functions like print() or str() are declared with a single argument "arg1: Variant", but that seems
551552
// to be a mistake. We change their parameter list by removing that.
@@ -571,7 +572,7 @@ impl UtilityFunction {
571572
parameters,
572573
return_value: FnReturn::new(&return_value, ctx),
573574
is_vararg: function.is_vararg,
574-
is_private: false,
575+
is_private,
575576
is_virtual_required: false,
576577
direction: FnDirection::Outbound {
577578
hash: function.hash,

godot-codegen/src/special_cases/special_cases.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,25 @@ pub fn is_builtin_type_scalar(name: &str) -> bool {
559559

560560
#[rustfmt::skip]
561561
pub fn is_utility_function_deleted(function: &JsonUtilityFunction, ctx: &mut Context) -> bool {
562-
/*let hardcoded = match function.name.as_str() {
563-
| "..."
562+
let hardcoded = match function.name.as_str() {
563+
// Removed in v0.3, but available as dedicated APIs.
564+
| "instance_from_id"
564565

565566
=> true, _ => false
566567
};
567568

568-
hardcoded ||*/ codegen_special_cases::is_utility_function_excluded(function, ctx)
569+
hardcoded || codegen_special_cases::is_utility_function_excluded(function, ctx)
570+
}
571+
572+
#[rustfmt::skip]
573+
pub fn is_utility_function_private(function: &JsonUtilityFunction) -> bool {
574+
match function.name.as_str() {
575+
// Removed from public interface in v0.3, but available as dedicated APIs.
576+
| "is_instance_valid" // used in Variant::is_object_alive().
577+
| "is_instance_id_valid" // used in InstanceId::lookup_validity().
578+
579+
=> true, _ => false
580+
}
569581
}
570582

571583
pub fn maybe_rename_class_method<'m>(class_name: &TyName, godot_method_name: &'m str) -> &'m str {

godot-core/src/builtin/variant/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl Variant {
274274
pub(crate) fn is_object_alive(&self) -> bool {
275275
debug_assert_eq!(self.get_type(), VariantType::OBJECT);
276276

277-
crate::gen::utilities::is_instance_valid(self)
277+
crate::global::is_instance_valid(self)
278278

279279
// In case there are ever problems with this approach, alternative implementation:
280280
// self.stringify() != "<Freed Object>".into()

godot-core/src/global/mod.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
//! - Variant: [`VariantType`][crate::builtin::VariantType], [`VariantOperator`][crate::builtin::VariantOperator]
2222
//! - Vector: [`Vector2Axis`][crate::builtin::Vector2Axis], [`Vector3Axis`][crate::builtin::Vector3Axis], [`Vector4Axis`][crate::builtin::Vector4Axis]
2323
//!
24+
//! # Functions moved to dedicated APIs
25+
//!
26+
//! Some methods in `@GlobalScope` are not directly available in `godot::global` module, but rather in their related types. \
27+
//! You can find them as follows:
28+
//!
29+
//! | Godot utility function | godot-rust APIs |
30+
//! |------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
31+
//! | `instance_from_id` | [`Gd::from_instance_id()`][crate::obj::Gd::from_instance_id]<br>[`Gd::try_from_instance_id()`][crate::obj::Gd::try_from_instance_id()] |
32+
//! | `is_instance_valid` | [`Gd::is_instance_valid()`][crate::obj::Gd::is_instance_valid()] |
33+
//! | `is_instance_id_valid` | [`InstanceId::lookup_validity()`][crate::obj::InstanceId::lookup_validity()] |
34+
//!
35+
36+
// Doc aliases are also available in dedicated APIs, but directing people here may give them a bit more context.
37+
#![doc(
38+
alias = "instance_from_id",
39+
alias = "is_instance_valid",
40+
alias = "is_instance_id_valid"
41+
)]
2442

2543
mod print;
2644

@@ -31,30 +49,8 @@ pub use crate::gen::central::global_enums::*;
3149
pub use crate::gen::utilities::*;
3250

3351
// This is needed for generated classes to find symbols, even those that have been moved to crate::builtin.
34-
use crate::builtin::Variant;
3552
#[allow(unused_imports)] // micromanaging imports for generated code is not fun
3653
pub(crate) use crate::builtin::{Corner, EulerOrder, Side};
37-
use crate::obj::Gd;
3854

3955
// ----------------------------------------------------------------------------------------------------------------------------------------------
4056
// 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/901."]
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/901."]
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/901."]
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/instance_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl InstanceId {
6666
/// This corresponds to Godot's global function `is_instance_id_valid()`.
6767
#[doc(alias = "is_instance_id_valid")]
6868
pub fn lookup_validity(self) -> bool {
69-
crate::gen::utilities::is_instance_id_valid(self.to_i64())
69+
crate::global::is_instance_id_valid(self.to_i64())
7070
}
7171

7272
// Private: see rationale above

itest/rust/src/engine_tests/utilities_test.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
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)]
8+
// Note: in the past, we had is_instance_valid() -- we also tested that godot-rust is not susceptible to the godot-cpp issue
9+
// https://github.com/godotengine/godot-cpp/issues/1390.
1010

1111
use crate::framework::itest;
1212

1313
use godot::builtin::{GString, Variant};
14-
use godot::classes::Node3D;
1514
use godot::global::*;
16-
use godot::obj::NewAlloc;
1715

1816
#[itest]
1917
fn utilities_abs() {
@@ -80,14 +78,3 @@ fn utilities_max() {
8078
);
8179
assert_eq!(output, Variant::from(-1.0));
8280
}
83-
84-
// Checks that godot-rust is not susceptible to the godot-cpp issue https://github.com/godotengine/godot-cpp/issues/1390.
85-
#[itest]
86-
fn utilities_is_instance_valid() {
87-
let node = Node3D::new_alloc();
88-
let variant = Variant::from(node.clone());
89-
assert!(is_instance_valid(variant.clone()));
90-
91-
node.free();
92-
assert!(!is_instance_valid(variant));
93-
}

itest/rust/src/object_tests/object_test.rs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use godot::classes::{
1616
file_access, Engine, FileAccess, IRefCounted, Node, Node2D, Node3D, Object, RefCounted,
1717
};
1818
#[allow(deprecated)]
19-
use godot::global::instance_from_id;
2019
use godot::meta::{FromGodot, GodotType, ToGodot};
2120
use godot::obj::{Base, Gd, Inherits, InstanceId, NewAlloc, NewGd, RawGd};
2221
use godot::register::{godot_api, GodotClass};
@@ -181,36 +180,6 @@ fn object_from_invalid_instance_id() {
181180
.expect_err("invalid instance id should not return a valid object");
182181
}
183182

184-
// `instance_from_id` is a normal FFI call, so works slightly differently from `Gd::try_from_instance_id`.
185-
#[itest]
186-
fn object_instance_from_id() {
187-
let node = Node::new_alloc();
188-
189-
assert!(node.is_instance_valid());
190-
191-
let instance_id = node.instance_id();
192-
193-
#[allow(deprecated)]
194-
let gd_from_instance_id = instance_from_id(instance_id.to_i64())
195-
.expect("instance should be valid")
196-
.cast::<Node>();
197-
198-
assert_eq!(gd_from_instance_id, node);
199-
200-
node.free();
201-
}
202-
203-
#[itest]
204-
fn object_instance_from_invalid_id() {
205-
#[allow(deprecated)]
206-
let gd_from_instance_id = instance_from_id(0);
207-
208-
assert!(
209-
gd_from_instance_id.is_none(),
210-
"instance id 0 should never be valid"
211-
);
212-
}
213-
214183
#[itest]
215184
fn object_from_instance_id_inherits_type() {
216185
let descr = GString::from("some very long description");
@@ -283,7 +252,14 @@ fn object_user_free_during_bind() {
283252
obj.is_instance_valid(),
284253
"object lives on after failed free()"
285254
);
255+
256+
let copy = obj.clone();
286257
obj.free(); // now succeeds
258+
259+
assert!(
260+
!copy.is_instance_valid(),
261+
"object is finally destroyed after successful free()"
262+
);
287263
}
288264

289265
#[itest]

0 commit comments

Comments
 (0)