pub struct Function<'lua>(/* private fields */);
Expand description
Handle to an internal Lua function.
Implementations§
source§impl<'lua> Function<'lua>
impl<'lua> Function<'lua>
sourcepub fn call<A, R>(&self, args: A) -> Result<R, Error>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
pub fn call<A, R>(&self, args: A) -> Result<R, Error>where
A: IntoLuaMulti<'lua>,
R: FromLuaMulti<'lua>,
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 bind<A>(&self, args: A) -> Result<Function<'lua>, Error>where
A: IntoLuaMulti<'lua>,
pub fn bind<A>(&self, args: A) -> Result<Function<'lua>, Error>where
A: IntoLuaMulti<'lua>,
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, Error>
pub fn set_environment(&self, env: Table<'_>) -> Result<bool, Error>
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.
source§impl<'lua> Function<'lua>
impl<'lua> Function<'lua>
sourcepub fn wrap<A, R, F>(func: F) -> impl IntoLua<'lua>where
A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'lua>,
F: Fn(&'lua Lua, A) -> Result<R, Error> + MaybeSend + 'static,
pub fn wrap<A, R, F>(func: F) -> impl IntoLua<'lua>where
A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'lua>,
F: Fn(&'lua Lua, A) -> Result<R, Error> + MaybeSend + 'static,
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<'lua>where
A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'lua>,
F: FnMut(&'lua Lua, A) -> Result<R, Error> + MaybeSend + 'static,
pub fn wrap_mut<A, R, F>(func: F) -> impl IntoLua<'lua>where
A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'lua>,
F: FnMut(&'lua Lua, A) -> Result<R, Error> + MaybeSend + 'static,
Wraps a Rust mutable closure, returning an opaque type that implements IntoLua
trait.
Trait Implementations§
Auto Trait Implementations§
impl<'lua> Freeze for Function<'lua>
impl<'lua> !RefUnwindSafe for Function<'lua>
impl<'lua> !Send for Function<'lua>
impl<'lua> !Sync for Function<'lua>
impl<'lua> Unpin for Function<'lua>
impl<'lua> !UnwindSafe for Function<'lua>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)