-
-
Notifications
You must be signed in to change notification settings - Fork 287
Register enums/bitfields in classes #890
Description
In Godot, a class can have integer constants as well as enums/bitfields. These enums and bitfields are largely just a convenient way of grouping together integer constants, since under the hood they are just integer constants and not fully-featured sum types like what rust has. From what i understand, enums and bitfields are largely the same from the perspective of ffi.
We have long had support for registering integer constants associated with a class, when this was added we also made the infrastructure needed to register enums and bitfields with a class. However it wasn't clear what syntax should be used to actually register them with the proc-macros. The most obvious syntax, something like
impl Foo {
enum Bar { .. }
}Does not work since enums cannot be declared in an impl block. This would be helped by inherent associated types since we could then declare a type alias in the impl block.
The main things to take into account for enums and bitfields are:
- These enums and bitfields must be registered with a specific class
- They can only contain integer variants
Some possible syntaxes:
#[derive(GodotClass)]
struct Foo { .. }
#[godot_enum_bikeshed_macro(Foo)]
#[repr(u8)]
enum Bar {
..
}#[derive(GodotClass)]
struct Foo { .. }
#[godot_api]
impl Foo {
#[enum(Bar)]
const A: u8 = 10;
#[enum(Bar)]
const B: u8 = 10;
}@Houtamelo made a declarative macro that emulates this:
gdscript_rust_enum! {
GDSCRIPT: TerrainVariant; // Name of the Godot class that will contain the constants
pub enum TerrainVariant {
Water = 0, // Integer values must be provided for each variant
Plains = 1,
}
}