macro_rules! function {
{
$(#[$($attr: tt)*])*
$lua: ident fn $name: ident(
$l: ident $(: $lty: ty)?
$(, $arg: ident : $aty: ty)*
) $(-> $ret: ty)? {
$($body: tt)*
}
} => { ... };
{
$(#[$($attr: tt)*])*
$lua: ident fn $source: ident $(::$inner: ident)* . $name: ident(
$l: ident $(: $lty: ty)?
$(, $arg: ident : $aty: ty)*
) $(-> $ret: ty)? {
$($body: tt)*
}
} => { ... };
}
Expand description
Write a lua function similar to Rust’s syntax
§Example
use mlua::Lua;
let lua = Lua::new();
lua.create_function(|lua, ()| Ok(()))
vs
use mlua_extras::{mlua::Lua, function};
let lua = Lua::new();
function! {
lua fn name(lua) {
Ok(())
}
}
It can also be used to asssign functions to nested tables. This requires the LuaExtras
crate
when you start with lua as the source, and the Require
trait when using any other table as
the source.
use mlua::{Lua, Table};
let lua = Lua::new();
lua.globals().get::<_, Table>("nested")?.set("name", lua.create_function(|lua, ()| Ok(()))?)?;
let nested = lua.globals().get::<_, Table>("deep")?.get::<_, Table>("nested");
nested.set("name", lua.create_function(|lua, ()| Ok(())))?;
vs
use mlua_extras::{
mlua::{Lua, Table},
extras::{LuaExtras, Require},
};
let lua = Lua::new();
function! {
lua fn lua::nested.name(lua) {
Ok(())
}
}
let nested = lua.globals().get::<_, Table>("deep")?;
function! {
lua fn deep::nested.name(lua) {
Ok(())
}
}