Skip to main content

LuaEnum

Trait LuaEnum 

Source
pub trait LuaEnum {
    // Required methods
    fn variants() -> &'static [(&'static str, i64)];
    fn enum_name() -> &'static str;
}
Expand description

Trait for Rust enums that can be exported to Lua as a table of constants.

Automatically implemented by #[derive(LuaUserData)] on C-like enums (enums with no data fields). Each variant becomes a key-value pair in a Lua table.

§Example

#[derive(LuaUserData)]
enum Color {
    Red,    // 0
    Green,  // 1
    Blue,   // 2
}

// With explicit discriminants:
#[derive(LuaUserData)]
enum HttpStatus {
    Ok = 200,
    NotFound = 404,
    ServerError = 500,
}

// Register in Lua:
vm.register_enum::<Color>("Color")?;
// Lua: Color.Red == 0, Color.Green == 1, Color.Blue == 2

Required Methods§

Source

fn variants() -> &'static [(&'static str, i64)]

Return variant name-value pairs for Lua table construction.

Source

fn enum_name() -> &'static str

Return the enum’s type name.

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.

Implementors§