godot_core/meta/inspect.rs
1/*
2 * Copyright (c) godot-rust; Bromeon and contributors.
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6 */
7
8//! Introspection metadata for Godot engine types.
9
10/// Metadata for a single enum or bitfield constant.
11///
12/// Returned by [`EngineEnum::all_constants()`][crate::obj::EngineEnum::all_constants] and
13/// [`EngineBitfield::all_constants()`][crate::obj::EngineBitfield::all_constants].
14#[derive(Copy, Clone, Eq, PartialEq, Debug)]
15pub struct EnumConstant<T: Copy + 'static> {
16 rust_name: &'static str,
17 godot_name: &'static str,
18 value: T,
19}
20
21impl<T> EnumConstant<T>
22where
23 T: Copy + Eq + PartialEq + 'static,
24{
25 /// Creates a new enum constant metadata entry.
26 pub(crate) const fn new(rust_name: &'static str, godot_name: &'static str, value: T) -> Self {
27 Self {
28 rust_name,
29 godot_name,
30 value,
31 }
32 }
33
34 /// Rust name of the constant, usually without prefix (e.g. `"ESCAPE"` for `Key::ESCAPE`).
35 ///
36 /// For enums, this is the value returned by [`EngineEnum::as_str()`](crate::obj::EngineEnum::as_str()) **if the value is unique.**
37 /// If multiple enum values share the same ordinal, then this function will return each one separately, while `as_str()` will return the
38 /// first one.
39 pub const fn rust_name(&self) -> &'static str {
40 self.rust_name
41 }
42
43 /// Godot constant name (e.g. `"KEY_ESCAPE"` for `Key::ESCAPE`).
44 pub const fn godot_name(&self) -> &'static str {
45 self.godot_name
46 }
47
48 /// The Rust value itself.
49 ///
50 /// Use `value().ord()` to get the ordinal value.
51 pub const fn value(&self) -> T {
52 self.value
53 }
54}