Skip to content

Commit 786f5dc

Browse files
committed
Reduce code duplication around TypeStringHint::type_string()
1 parent 11c2ae0 commit 786f5dc

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

godot-core/src/builtin/array.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use godot_ffi as sys;
99

1010
use crate::builtin::*;
11-
use crate::property::{Export, PropertyHintInfo, TypeStringHint, Var};
11+
use crate::property::{builtin_type_string, Export, PropertyHintInfo, TypeStringHint, Var};
1212
use std::fmt;
1313
use std::marker::PhantomData;
1414
use sys::{ffi_methods, interface_fn, GodotFfi};
@@ -834,11 +834,7 @@ impl<T: ArrayElement + TypeStringHint> TypeStringHint for Array<T> {
834834

835835
impl TypeStringHint for VariantArray {
836836
fn type_string() -> String {
837-
if sys::GdextBuild::since_api("4.3") {
838-
format!("{}:", VariantType::Array as i32)
839-
} else {
840-
format!("{}:Array", VariantType::Array as i32)
841-
}
837+
builtin_type_string::<VariantArray>()
842838
}
843839
}
844840

godot-core/src/builtin/dictionary.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ use godot_ffi as sys;
99

1010
use crate::builtin::meta::{FromGodot, ToGodot};
1111
use crate::builtin::{inner, Variant, VariantArray};
12-
use crate::property::{Export, PropertyHintInfo, TypeStringHint, Var};
13-
use std::marker::PhantomData;
14-
use std::{fmt, ptr};
12+
use crate::property::{builtin_type_string, Export, PropertyHintInfo, TypeStringHint, Var};
1513
use sys::types::OpaqueDictionary;
1614
use sys::{ffi_methods, interface_fn, GodotFfi};
1715

16+
use std::marker::PhantomData;
17+
use std::{fmt, ptr};
18+
1819
use super::meta::impl_godot_as_self;
1920

2021
/// Godot's `Dictionary` type.
@@ -330,11 +331,7 @@ impl Var for Dictionary {
330331

331332
impl TypeStringHint for Dictionary {
332333
fn type_string() -> String {
333-
if sys::GdextBuild::since_api("4.3") {
334-
format!("{}:", sys::VariantType::Dictionary as i32)
335-
} else {
336-
format!("{}:Dictionary", sys::VariantType::Dictionary as i32)
337-
}
334+
builtin_type_string::<Dictionary>()
338335
}
339336
}
340337

godot-core/src/builtin/packed_array.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,15 +481,16 @@ macro_rules! impl_packed_array {
481481

482482
impl $crate::property::Export for $PackedArray {
483483
fn default_export_info() -> $crate::property::PropertyHintInfo {
484+
// In 4.3 Godot can (and does) use type hint strings for packed arrays, see https://github.com/godotengine/godot/pull/82952.
484485
if sys::GdextBuild::since_api("4.3") {
485-
// In 4.3 Godot can (and does) use type hint strings for packed arrays.
486-
// https://github.com/godotengine/godot/pull/82952
487486
$crate::property::PropertyHintInfo {
488487
hint: $crate::engine::global::PropertyHint::TYPE_STRING,
489488
hint_string: <$Element as $crate::property::TypeStringHint>::type_string().into(),
490489
}
491490
} else {
492-
$crate::property::PropertyHintInfo::with_hint_none(<$PackedArray as $crate::builtin::meta::GodotType>::godot_type_name())
491+
$crate::property::PropertyHintInfo::with_hint_none(
492+
<$PackedArray as $crate::builtin::meta::GodotType>::godot_type_name()
493+
)
493494
}
494495
}
495496
}

godot-core/src/registry/property.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
use godot_ffi as sys;
1111

12-
use crate::builtin::meta::{FromGodot, GodotConvert, ToGodot};
12+
use crate::builtin::meta::{FromGodot, GodotConvert, GodotType, ToGodot};
1313
use crate::builtin::GString;
1414
use crate::engine::global::PropertyHint;
1515

@@ -336,7 +336,6 @@ pub mod export_info_functions {
336336
mod export_impls {
337337
use super::*;
338338
use crate::builtin::*;
339-
use godot_ffi as sys;
340339

341340
macro_rules! impl_property_by_godot_convert {
342341
($Ty:ty, no_export) => {
@@ -373,15 +372,7 @@ mod export_impls {
373372
(@type_string_hint $Ty:ty) => {
374373
impl TypeStringHint for $Ty {
375374
fn type_string() -> String {
376-
use sys::GodotFfi as _;
377-
let variant_type = <$Ty as $crate::builtin::meta::GodotType>::Ffi::variant_type();
378-
379-
if sys::GdextBuild::since_api("4.3") {
380-
format!("{}:", variant_type as i32)
381-
} else {
382-
let type_name = <$Ty as $crate::builtin::meta::GodotType>::godot_type_name();
383-
format!("{}:{}", variant_type as i32, type_name)
384-
}
375+
builtin_type_string::<$Ty>()
385376
}
386377
}
387378
}
@@ -467,3 +458,19 @@ mod export_impls {
467458

468459
// impl_property_by_godot_convert!(Signal);
469460
}
461+
462+
// ----------------------------------------------------------------------------------------------------------------------------------------------
463+
// Crate-local utilities
464+
465+
pub(crate) fn builtin_type_string<T: GodotType>() -> String {
466+
use sys::GodotFfi as _;
467+
468+
let variant_type = T::Ffi::variant_type();
469+
470+
// Godot 4.3 changed representation for type hints, see https://github.com/godotengine/godot/pull/90716.
471+
if sys::GdextBuild::since_api("4.3") {
472+
format!("{}:", variant_type.sys())
473+
} else {
474+
format!("{}:{}", variant_type.sys(), T::godot_type_name())
475+
}
476+
}

itest/rust/src/object_tests/property_template_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ fn property_template_test(ctx: &TestContext) {
3333
for property in rust_properties.get_property_list().iter_shared() {
3434
let name = property.get("name").unwrap().to::<String>();
3535

36-
// The format of array-properties in Godot 4.2 changed. This doesn't seem to cause issues if we
37-
// compile against 4.1 and provide the property in the format 4.1 expects but run it with Godot 4.2.
38-
// However this test checks that our output matches that of Godot, and so would fail in this
39-
// circumstance. So here for now, just ignore array properties when we compile for 4.1 but run in 4.2.
36+
// The format of array properties in Godot 4.2 changed. This doesn't seem to cause issues if we
37+
// compile against 4.1 and provide the property in the format 4.1 expects, but run it with Godot 4.2.
38+
// However, this test checks that our output matches that of Godot, and so would fail in this circumstance.
39+
// For now, just ignore array properties when we compile for 4.1 but run in 4.2.
4040
if GdextBuild::since_api("4.2")
4141
&& cfg!(before_api = "4.2")
4242
&& name.starts_with("property_array_")

0 commit comments

Comments
 (0)