pub struct Function(/* private fields */);
Expand description
Handle to an internal Lua function.
Implementations§
Source§impl Function
impl Function
Sourcepub fn call<R: FromLuaMulti>(&self, args: impl IntoLuaMulti) -> Result<R>
pub fn call<R: FromLuaMulti>(&self, args: impl IntoLuaMulti) -> Result<R>
Calls the function, passing args
as function arguments.
The function’s return values are converted to the generic type R
.
§Examples
Call Lua’s built-in tostring
function:
let globals = lua.globals();
let tostring: Function = globals.get("tostring")?;
assert_eq!(tostring.call::<String>(123)?, "123");
Call a function with multiple arguments:
let sum: Function = lua.load(
r#"
function(a, b)
return a + b
end
"#).eval()?;
assert_eq!(sum.call::<u32>((3, 4))?, 3 + 4);
Sourcepub fn call_async<R>(
&self,
args: impl IntoLuaMulti,
) -> impl Future<Output = Result<R>>where
R: FromLuaMulti,
Available on crate feature async
only.
pub fn call_async<R>(
&self,
args: impl IntoLuaMulti,
) -> impl Future<Output = Result<R>>where
R: FromLuaMulti,
async
only.Returns a future that, when polled, calls self
, passing args
as function arguments,
and drives the execution.
Internally it wraps the function to an AsyncThread
.
Requires feature = "async"
§Examples
use std::time::Duration;
let sleep = lua.create_async_function(move |_lua, n: u64| async move {
tokio::time::sleep(Duration::from_millis(n)).await;
Ok(())
})?;
sleep.call_async(10).await?;
Sourcepub fn bind(&self, args: impl IntoLuaMulti) -> Result<Function>
pub fn bind(&self, args: impl IntoLuaMulti) -> Result<Function>
Returns a function that, when called, calls self
, passing args
as the first set of
arguments.
If any arguments are passed to the returned function, they will be passed after args
.
§Examples
let sum: Function = lua.load(
r#"
function(a, b)
return a + b
end
"#).eval()?;
let bound_a = sum.bind(1)?;
assert_eq!(bound_a.call::<u32>(2)?, 1 + 2);
let bound_a_and_b = sum.bind(13)?.bind(57)?;
assert_eq!(bound_a_and_b.call::<u32>(())?, 13 + 57);
Sourcepub fn environment(&self) -> Option<Table>
pub fn environment(&self) -> Option<Table>
Returns the environment of the Lua function.
By default Lua functions shares a global environment.
This function always returns None
for Rust/C functions.
Sourcepub fn set_environment(&self, env: Table) -> Result<bool>
pub fn set_environment(&self, env: Table) -> Result<bool>
Sets the environment of the Lua function.
The environment is a table that is used as the global environment for the function.
Returns true
if environment successfully changed, false
otherwise.
This function does nothing for Rust/C functions.
Sourcepub fn info(&self) -> FunctionInfo
pub fn info(&self) -> FunctionInfo
Returns information about the function.
Corresponds to the >Sn
what mask for lua_getinfo
when applied to the function.
Sourcepub fn dump(&self, strip: bool) -> Vec<u8> ⓘ
Available on non-crate feature luau
only.
pub fn dump(&self, strip: bool) -> Vec<u8> ⓘ
luau
only.Dumps the function as a binary chunk.
If strip
is true, the binary representation may not include all debug information
about the function, to save space.
For Luau a Compiler can be used to compile Lua chunks to bytecode.
Sourcepub fn coverage<F>(&self, func: F)where
F: FnMut(CoverageInfo),
Available on crate feature luau
only.
pub fn coverage<F>(&self, func: F)where
F: FnMut(CoverageInfo),
luau
only.Retrieves recorded coverage information about this Lua function including inner calls.
This function takes a callback as an argument and calls it providing CoverageInfo
snapshot per each executed inner function.
Recording of coverage information is controlled by Compiler::set_coverage_level
option.
Requires feature = "luau"
Sourcepub fn to_pointer(&self) -> *const c_void
pub fn to_pointer(&self) -> *const c_void
Converts this function to a generic C pointer.
There is no way to convert the pointer back to its original value.
Typically this function is used only for hashing and debug information.
Source§impl Function
impl Function
Sourcepub fn wrap<A, R, F>(func: F) -> impl IntoLua
pub fn wrap<A, R, F>(func: F) -> impl IntoLua
Wraps a Rust function or closure, returning an opaque type that implements IntoLua
trait.
Sourcepub fn wrap_mut<A, R, F>(func: F) -> impl IntoLua
pub fn wrap_mut<A, R, F>(func: F) -> impl IntoLua
Wraps a Rust mutable closure, returning an opaque type that implements IntoLua
trait.
Sourcepub fn wrap_async<A, R, F, FR>(func: F) -> impl IntoLuawhere
A: FromLuaMulti,
R: IntoLuaMulti,
F: Fn(Lua, A) -> FR + MaybeSend + 'static,
FR: Future<Output = Result<R>> + MaybeSend + 'static,
Available on crate feature async
only.
pub fn wrap_async<A, R, F, FR>(func: F) -> impl IntoLuawhere
A: FromLuaMulti,
R: IntoLuaMulti,
F: Fn(Lua, A) -> FR + MaybeSend + 'static,
FR: Future<Output = Result<R>> + MaybeSend + 'static,
async
only.Wraps a Rust async function or closure, returning an opaque type that implements IntoLua
trait.