load

Macro load 

Source
load!() { /* proc-macro */ }
Expand description

Registers one or more Rust types implementing mlua::UserData as global variables in a mlua::Lua instance.

This macro is the final step to make your Rust types accessible from Lua. It creates a “proxy” for each type (which acts as a constructor table) and assigns it to a global variable in Lua with the same name as the Rust type.

§Usage

The macro takes the lua instance as the first argument, followed by a comma-separated list of types to register.

// (Assuming Player and PlayerStatus implement mlua::UserData)
use mlua::prelude::*;

fn main() -> LuaResult<()> {
    let lua = Lua::new();

    // This call...
    mlua_magic_macros::load!(lua, Player, PlayerStatus);

    // ...is equivalent to this Lua code:
    // Player = (proxy for Player UserData)
    // PlayerStatus = (proxy for PlayerStatus UserData)

    lua.load(r#"
        print(Player)       -- "userdata: Player"
        print(PlayerStatus) -- "userdata: PlayerStatus"

        local p = Player.new("Hero")
        p.status = PlayerStatus.Walking()
    "#).exec()?;

    Ok(())
}

§Prerequisites

All types passed to load! must implement mlua::UserData. This is typically handled by using the mlua_magic_macros::compile! macro.