Skip to content

Commit 96f2c32

Browse files
committed
Deprecate EngineEnum::godot_name()
1 parent 32f30b6 commit 96f2c32

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

godot-core/src/meta/inspect.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ pub struct EnumConstant<T: Copy + 'static> {
1818
value: T,
1919
}
2020

21-
impl<T: Copy + 'static> EnumConstant<T> {
21+
impl<T> EnumConstant<T>
22+
where
23+
T: Copy + Eq + PartialEq + 'static,
24+
{
2225
/// Creates a new enum constant metadata entry.
2326
pub(crate) const fn new(rust_name: &'static str, godot_name: &'static str, value: T) -> Self {
2427
Self {

godot-core/src/obj/traits.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,25 @@ pub trait EngineEnum: Copy {
201201
/// The equivalent name of the enumerator, as specified in Godot.
202202
///
203203
/// If the value does not match one of the known enumerators, the empty string is returned.
204+
///
205+
/// # Deprecation
206+
/// Design change is due to the fact that Godot enums may have multiple constants with the same ordinal value, and `godot_name()` cannot
207+
/// always return a unique name for it. So there are cases where this method returns unexpected results.
208+
///
209+
/// To keep the old -- possibly incorrect -- behavior, you can write the following function. However, it runs in linear rather than constant
210+
/// time (which is often OK, given that there are very few constants per enum).
211+
/// ```
212+
/// use godot::obj::EngineEnum;
213+
///
214+
/// fn godot_name<T: EngineEnum + Eq + PartialEq + 'static>(value: T) -> &'static str {
215+
/// T::all_constants()
216+
/// .iter()
217+
/// .find(|c| c.value() == value)
218+
/// .map(|c| c.godot_name())
219+
/// .unwrap_or("") // Previous behavior.
220+
/// }
221+
/// ```
222+
#[deprecated = "Moved to introspection API, see `EngineEnum::all_constants()` and `EnumConstant::godot_name()`"]
204223
fn godot_name(&self) -> &'static str;
205224

206225
/// Returns a slice of distinct enum values.

itest/rust/src/object_tests/enum_test.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use godot::classes::input::CursorShape;
1111
use godot::classes::mesh::PrimitiveType;
1212
use godot::classes::{time, ArrayMesh};
1313
use godot::global::{Key, Orientation};
14-
use godot::obj::NewGd;
14+
use godot::obj::{EngineEnum, NewGd};
1515
use std::collections::HashSet;
1616

1717
#[itest]
@@ -88,6 +88,29 @@ fn enum_as_str() {
8888

8989
#[itest]
9090
fn enum_godot_name() {
91+
use godot::obj::EngineEnum;
92+
assert_eq!(
93+
godot_name(Orientation::VERTICAL),
94+
Orientation::VERTICAL.as_str()
95+
);
96+
assert_eq!(
97+
godot_name(Orientation::HORIZONTAL),
98+
Orientation::HORIZONTAL.as_str()
99+
);
100+
101+
assert_eq!(godot_name(Key::NONE), "KEY_NONE");
102+
assert_eq!(godot_name(Key::SPECIAL), "KEY_SPECIAL");
103+
assert_eq!(godot_name(Key::ESCAPE), "KEY_ESCAPE");
104+
assert_eq!(godot_name(Key::TAB), "KEY_TAB");
105+
assert_eq!(godot_name(Key::A), "KEY_A");
106+
107+
// Unknown enumerators (might come from the future).
108+
assert_eq!(godot_name(Key::from_ord(1234)), "");
109+
}
110+
111+
#[itest]
112+
#[expect(deprecated)]
113+
fn enum_godot_name_deprecated() {
91114
use godot::obj::EngineEnum;
92115
assert_eq!(
93116
Orientation::VERTICAL.godot_name(),
@@ -103,4 +126,15 @@ fn enum_godot_name() {
103126
assert_eq!(Key::ESCAPE.godot_name(), "KEY_ESCAPE");
104127
assert_eq!(Key::TAB.godot_name(), "KEY_TAB");
105128
assert_eq!(Key::A.godot_name(), "KEY_A");
129+
130+
// Unknown enumerators (might come from the future).
131+
assert_eq!(Key::from_ord(1234).godot_name(), "");
132+
}
133+
134+
fn godot_name<T: EngineEnum + Eq + PartialEq + 'static>(value: T) -> &'static str {
135+
T::all_constants()
136+
.iter()
137+
.find(|c| c.value() == value)
138+
.map(|c| c.godot_name())
139+
.unwrap_or("") // Previous behavior.
106140
}

0 commit comments

Comments
 (0)