Skip to content

Commit e0b7874

Browse files
committed
Deprecate EngineEnum::godot_name()
1 parent 03134bb commit e0b7874

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
@@ -12,7 +12,7 @@ use godot::classes::mesh::PrimitiveType;
1212
use godot::classes::window::LayoutDirection;
1313
use godot::classes::{time, ArrayMesh};
1414
use godot::global::{Key, Orientation};
15-
use godot::obj::NewGd;
15+
use godot::obj::{EngineEnum, NewGd};
1616
use std::collections::HashSet;
1717

1818
#[itest]
@@ -92,6 +92,29 @@ fn enum_as_str() {
9292

9393
#[itest]
9494
fn enum_godot_name() {
95+
use godot::obj::EngineEnum;
96+
assert_eq!(
97+
godot_name(Orientation::VERTICAL),
98+
Orientation::VERTICAL.as_str()
99+
);
100+
assert_eq!(
101+
godot_name(Orientation::HORIZONTAL),
102+
Orientation::HORIZONTAL.as_str()
103+
);
104+
105+
assert_eq!(godot_name(Key::NONE), "KEY_NONE");
106+
assert_eq!(godot_name(Key::SPECIAL), "KEY_SPECIAL");
107+
assert_eq!(godot_name(Key::ESCAPE), "KEY_ESCAPE");
108+
assert_eq!(godot_name(Key::TAB), "KEY_TAB");
109+
assert_eq!(godot_name(Key::A), "KEY_A");
110+
111+
// Unknown enumerators (might come from the future).
112+
assert_eq!(godot_name(Key::from_ord(1234)), "");
113+
}
114+
115+
#[itest]
116+
#[expect(deprecated)]
117+
fn enum_godot_name_deprecated() {
95118
use godot::obj::EngineEnum;
96119
assert_eq!(
97120
Orientation::VERTICAL.godot_name(),
@@ -107,4 +130,15 @@ fn enum_godot_name() {
107130
assert_eq!(Key::ESCAPE.godot_name(), "KEY_ESCAPE");
108131
assert_eq!(Key::TAB.godot_name(), "KEY_TAB");
109132
assert_eq!(Key::A.godot_name(), "KEY_A");
133+
134+
// Unknown enumerators (might come from the future).
135+
assert_eq!(Key::from_ord(1234).godot_name(), "");
136+
}
137+
138+
fn godot_name<T: EngineEnum + Eq + PartialEq + 'static>(value: T) -> &'static str {
139+
T::all_constants()
140+
.iter()
141+
.find(|c| c.value() == value)
142+
.map(|c| c.godot_name())
143+
.unwrap_or("") // Previous behavior.
110144
}

0 commit comments

Comments
 (0)