Skip to main content

LuaUserData

Derive Macro LuaUserData 

Source
#[derive(LuaUserData)]
{
    // Attributes available to this derive:
    #[lua]
    #[lua_impl]
}
Expand description

Derive UserDataTrait for a struct or enum.

§Supported field types (auto-converted to/from UdValue)

  • i8..i64, isizeUdValue::Integer
  • u8..u64, usizeUdValue::Integer
  • f32, f64UdValue::Number
  • boolUdValue::Boolean
  • StringUdValue::Str

§Field attributes

  • #[lua(skip)] — exclude from Lua
  • #[lua(readonly)] — get only, no set
  • #[lua(name = "...")] — custom Lua name

§Struct attributes

  • #[lua_impl(Display, PartialEq, PartialOrd)] — metamethods from Rust traits

§Enum behavior

  • C-like enums also implement LuaEnum, so they can be exported with register_enum::<T>()
  • Enums with payloads are treated as fieldless userdata and can expose methods via #[lua_methods]

§Conversion behavior

  • IntoLua is auto-implemented, so derived userdata values can be passed directly into typed APIs
  • Owned FromLua is intentionally not auto-implemented, because userdata lives in Lua GC storage and implicit extraction by value would blur ownership semantics

§Example

#[derive(LuaUserData, PartialEq, PartialOrd)]
#[lua_impl(Display, PartialEq, PartialOrd)]
struct Point {
    pub x: f64,
    pub y: f64,
    #[lua(skip)]
    internal_id: u32,
}