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: ToLuaMulti<'lua>, R: FromLuaMulti<'lua>>(
&self,
args: A
) -> Result<R>
pub fn call<A: ToLuaMulti<'lua>, R: FromLuaMulti<'lua>>( &self, args: A ) -> 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_context.globals();
let tostring: Function = globals.get("tostring")?;
assert_eq!(tostring.call::<_, String>(123)?, "123");
Call a function with multiple arguments:
let sum: Function = lua_context.load(
r#"
function(a, b)
return a + b
end
"#).eval()?;
assert_eq!(sum.call::<_, u32>((3, 4))?, 3 + 4);
sourcepub fn bind<A: ToLuaMulti<'lua>>(&self, args: A) -> Result<Function<'lua>>
pub fn bind<A: ToLuaMulti<'lua>>(&self, args: A) -> Result<Function<'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_context.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 dump(&self) -> Result<Vec<u8>>
pub fn dump(&self) -> Result<Vec<u8>>
Dumps the compiled representation of the function into a binary blob, which can later be loaded using the unsafe Chunk::into_function_allow_binary().
Examples
let add2: Function = lua_context.load(r#"
function(a)
return a + 2
end
"#).eval()?;
let dumped = add2.dump()?;
let reloaded = unsafe {
lua_context.load(&dumped)
.into_function_allow_binary()?
};
assert_eq!(reloaded.call::<_, u32>(7)?, 7+2);
Trait Implementations§
Auto Trait Implementations§
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
Mutably borrows from an owned value. Read more
source§impl<'lua, T> FromLuaMulti<'lua> for Twhere
T: FromLua<'lua>,
impl<'lua, T> FromLuaMulti<'lua> for Twhere
T: FromLua<'lua>,
source§fn from_lua_multi(
values: MultiValue<'lua>,
lua: Context<'lua>
) -> Result<T, Error>
fn from_lua_multi( values: MultiValue<'lua>, lua: Context<'lua> ) -> Result<T, Error>
Performs the conversion. Read more
source§impl<'lua, T> ToLuaMulti<'lua> for Twhere
T: ToLua<'lua>,
impl<'lua, T> ToLuaMulti<'lua> for Twhere
T: ToLua<'lua>,
source§fn to_lua_multi(self, lua: Context<'lua>) -> Result<MultiValue<'lua>, Error>
fn to_lua_multi(self, lua: Context<'lua>) -> Result<MultiValue<'lua>, Error>
Performs the conversion.