Struct hlua::LuaFunction
source · pub struct LuaFunction<L> { /* private fields */ }
Expand description
Handle to a function in the Lua context.
Just like you can read variables as integers and strings, you can also read Lua functions by
requesting a LuaFunction
object. Once you have a LuaFunction
you can call it with call()
.
Note: Passing parameters when calling the function is not yet implemented.
§Example
let mut lua = hlua::Lua::new();
lua.execute::<()>("function foo() return 12 end").unwrap();
let mut foo: hlua::LuaFunction<_> = lua.get("foo").unwrap();
let result: i32 = foo.call().unwrap();
assert_eq!(result, 12);
Implementations§
source§impl<'lua, L> LuaFunction<L>where
L: AsMutLua<'lua>,
impl<'lua, L> LuaFunction<L>where
L: AsMutLua<'lua>,
sourcepub fn call<'a, V>(&'a mut self) -> Result<V, LuaError>
pub fn call<'a, V>(&'a mut self) -> Result<V, LuaError>
Calls the function. Doesn’t allow passing parameters.
TODO: will eventually disappear and get replaced with call_with_args
Returns an error if there is an error while executing the Lua code (eg. a function call returns an error), or if the requested return type doesn’t match the actual return type.
Note: In order to pass parameters, see
call_with_args
instead.
sourcepub fn call_with_args<'a, V, A, E>(
&'a mut self,
args: A
) -> Result<V, LuaFunctionCallError<E>>
pub fn call_with_args<'a, V, A, E>( &'a mut self, args: A ) -> Result<V, LuaFunctionCallError<E>>
Calls the function with parameters.
TODO: should be eventually be renamed to call
You can either pass a single value by passing a single value, or multiple parameters by passing a tuple. If you pass a tuple, the first element of the tuple will be the first argument, the second element of the tuple the second argument, and so on.
Returns an error if there is an error while executing the Lua code (eg. a function call returns an error), if the requested return type doesn’t match the actual return type, or if we failed to push an argument.
§Example
let mut lua = hlua::Lua::new();
lua.execute::<()>("function sub(a, b) return a - b end").unwrap();
let mut foo: hlua::LuaFunction<_> = lua.get("sub").unwrap();
let result: i32 = foo.call_with_args((18, 4)).unwrap();
assert_eq!(result, 14);
sourcepub fn load_from_reader<R>(
lua: L,
code: R
) -> Result<LuaFunction<PushGuard<L>>, LuaError>where
R: Read,
pub fn load_from_reader<R>(
lua: L,
code: R
) -> Result<LuaFunction<PushGuard<L>>, LuaError>where
R: Read,
Builds a new LuaFunction
from the code of a reader.
Returns an error if reading from the Read
object fails or if there is a syntax error in
the code.
§Example
use std::io::Cursor;
let mut lua = hlua::Lua::new();
let mut f = hlua::LuaFunction::load_from_reader(&mut lua, Cursor::new("return 8")).unwrap();
let ret: i32 = f.call().unwrap();
assert_eq!(ret, 8);