#[implementation]Expand description
Implements a helper function _to_mlua_methods for a Rust impl block,
enabling automatic registration of its methods with mlua::UserData.
When applied to an impl block, this macro scans for functions and
generates an implementation of a private helper function. This function
is later invoked by the mlua_magic_macros::compile! macro.
§Behavior
- Static Functions (e.g.,
fn new() -> Self) are registered as static functions on the userdata, accessible in Lua asMyType.new(). - Immutable Methods (e.g.,
fn my_method(&self)) are registered as immutable methods, accessible in Lua asmy_instance:my_method(). - Mutable Methods (e.g.,
fn my_mut_method(&mut self)) are registered as mutable methods, accessible in Lua asmy_instance:my_mut_method().
§Usage
Apply the macro directly to the impl block for the type:
ⓘ
#[mlua_magic_macros::structure]
struct Player { hp: i32 }
#[mlua_magic_macros::implementation]
impl Player {
pub fn new() -> Self { Self { hp: 100 } }
pub fn is_alive(&self) -> bool { self.hp > 0 }
pub fn take_damage(&mut self, amount: i32) { self.hp -= amount; }
}
// Later, compile userdata:
mlua_magic_macros::compile!(Player, fields, methods);Lua scripts may then call these methods:
local p = Player.new()
p:take_damage(30)
print(p:is_alive())This macro is designed to work together with:
#[mlua_magic_macros::structure]— for fields#[mlua_magic_macros::enumeration]— for enum variantsmlua_magic_macros::compile!— final hookup tomlua::UserData