pub trait EngineEnum: Copy {
// Required methods
fn try_from_ord(ord: i32) -> Option<Self>;
fn ord(self) -> i32;
fn as_str(&self) -> &'static str;
fn godot_name(&self) -> &'static str;
fn values() -> &'static [Self];
fn all_constants() -> &'static [EnumConstant<Self>];
// Provided method
fn from_ord(ord: i32) -> Self { ... }
}
Expand description
Auto-implemented for all engine-provided enums.
Required Methodsยง
fn try_from_ord(ord: i32) -> Option<Self>
Sourcefn ord(self) -> i32
fn ord(self) -> i32
Ordinal value of the enumerator, as specified in Godot. This is not necessarily unique.
Sourcefn as_str(&self) -> &'static str
fn as_str(&self) -> &'static str
The name of the enumerator, as it appears in Rust.
Note that this may not match the Rust constant name. In case of multiple constants with the same ordinal value, this method returns
the first one in the order of definition. For example, LayoutDirection::LOCALE.as_str()
(ord 1) returns "APPLICATION_LOCALE"
, because that happens to be the first constant with ordinal 1
.
See all_constants()
for a more robust and general approach to introspection of enum constants.
If the value does not match one of the known enumerators, the empty string is returned.
Sourcefn godot_name(&self) -> &'static str
๐Deprecated: Moved to introspection API, see EngineEnum::all_constants()
and EnumConstant::godot_name()
fn godot_name(&self) -> &'static str
EngineEnum::all_constants()
and EnumConstant::godot_name()
The equivalent name of the enumerator, as specified in Godot.
If the value does not match one of the known enumerators, the empty string is returned.
ยงDeprecation
Design change is due to the fact that Godot enums may have multiple constants with the same ordinal value, and godot_name()
cannot
always return a unique name for it. So there are cases where this method returns unexpected results.
To keep the old โ possibly incorrect โ behavior, you can write the following function. However, it runs in linear rather than constant time (which is often OK, given that there are very few constants per enum).
use godot::obj::EngineEnum;
fn godot_name<T: EngineEnum + Eq + PartialEq + 'static>(value: T) -> &'static str {
T::all_constants()
.iter()
.find(|c| c.value() == value)
.map(|c| c.godot_name())
.unwrap_or("") // Previous behavior.
}
Sourcefn values() -> &'static [Self]
fn values() -> &'static [Self]
Returns a slice of distinct enum values.
This excludes MAX
constants at the end (existing only to express the number of enumerators) and deduplicates aliases,
providing only meaningful enum values. See all_constants()
for a complete list of all constants.
Enables iteration over distinct enum variants:
use godot::classes::window;
use godot::obj::EngineEnum;
for mode in window::Mode::values() {
println!("* {}: {}", mode.as_str(), mode.ord());
}
Sourcefn all_constants() -> &'static [EnumConstant<Self>]
fn all_constants() -> &'static [EnumConstant<Self>]
Returns metadata for all enum constants.
This includes all constants as they appear in the enum definition, including duplicates and MAX
constants.
For a list of useful, distinct values, use values()
.
Enables introspection of available constants:
use godot::classes::window;
use godot::obj::EngineEnum;
for constant in window::Mode::all_constants() {
println!("* window::Mode.{} (original {}) has ordinal value {}.",
constant.rust_name(),
constant.godot_name(),
constant.value().ord()
);
}
Provided Methodsยง
Dyn Compatibilityยง
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.