Attribute Macro mlua::lua_module

source ·
#[lua_module]
Available on crate feature module only.
Expand description

Registers Lua module entrypoint.

You can register multiple entrypoints as required.

use mlua::{Lua, Result, Table};

#[mlua::lua_module]
fn my_module(lua: &Lua) -> Result<Table> {
    let exports = lua.create_table()?;
    exports.set("hello", "world")?;
    Ok(exports)
}

Internally in the code above the compiler defines C function luaopen_my_module.

You can also pass options to the attribute:

name - name of the module, defaults to the name of the function

#[mlua::lua_module(name = "alt_module")]
fn my_module(lua: &Lua) -> Result<Table> {
    ...
}