Expand description
Lua function handling.
This module provides types for working with Lua functions from Rust, including both Lua-defined functions and native Rust callbacks.
§Main Types
Function- A handle to a Lua function that can be called from Rust.FunctionInfo- Debug information about a function (name, source, line numbers, etc.).CoverageInfo- Code coverage data for Luau functions (requiresluaufeature).
§Calling Functions
Use Function::call to invoke a Lua function synchronously:
let lua = Lua::new();
// Get a built-in function
let print: Function = lua.globals().get("print")?;
print.call::<()>("Hello from Rust!")?;
// Call a function that returns values
let tonumber: Function = lua.globals().get("tonumber")?;
let n: i32 = tonumber.call("42")?;
assert_eq!(n, 42);For asynchronous execution, use Function::call_async (requires async feature):
ⓘ
let result: String = my_async_func.call_async(args).await?;§Creating Functions
Functions can be created from Rust closures using Lua::create_function:
let lua = Lua::new();
let greet = lua.create_function(|_, name: String| {
Ok(format!("Hello, {}!", name))
})?;
lua.globals().set("greet", greet)?;
let result: String = lua.load(r#"greet("World")"#).eval()?;
assert_eq!(result, "Hello, World!");For simpler cases, use Function::wrap or Function::wrap_raw to convert a Rust function
directly:
let lua = Lua::new();
fn add(a: i32, b: i32) -> i32 { a + b }
lua.globals().set("add", Function::wrap_raw(add))?;
let sum: i32 = lua.load("add(2, 3)").eval()?;
assert_eq!(sum, 5);§Function Environments
Lua functions have an associated environment table that determines how global
variables are resolved. Use Function::environment and Function::set_environment
to inspect or modify this environment.
Structs§
- Async
Call Future async - Future for asynchronous function calls.
- Coverage
Info luau - Luau function coverage snapshot.
- Function
- Handle to an internal Lua function.
- Function
Info - Contains information about a function.