Struct hlua_badtouch::LuaFunction
[−]
[src]
pub struct LuaFunction<L> { /* fields omitted */ }
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);
Methods
impl<'lua, L> LuaFunction<L> where
L: AsMutLua<'lua>,
[src]
L: AsMutLua<'lua>,
pub fn call<'a, V>(&'a mut self) -> Result<V, LuaError> where
V: LuaRead<PushGuard<&'a mut L>>,
[src]
V: LuaRead<PushGuard<&'a mut L>>,
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.
pub fn call_with_args<'a, V, A, E>(
&'a mut self,
args: A
) -> Result<V, LuaFunctionCallError<E>> where
A: for<'r> Push<&'r mut LuaFunction<L>, Err = E>,
V: LuaRead<PushGuard<&'a mut L>>,
[src]
&'a mut self,
args: A
) -> Result<V, LuaFunctionCallError<E>> where
A: for<'r> Push<&'r mut LuaFunction<L>, Err = E>,
V: LuaRead<PushGuard<&'a mut L>>,
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);
pub fn load_from_reader<R>(
lua: L,
code: R
) -> Result<LuaFunction<PushGuard<L>>, LuaError> where
R: Read,
[src]
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);
pub fn load(lua: L, code: &str) -> Result<LuaFunction<PushGuard<L>>, LuaError>
[src]
Builds a new LuaFunction
from a raw string.
Note: This is just a wrapper around
load_from_reader
. There is no advantage in usingload
except that it is more convenient.
Trait Implementations
impl<L: Debug> Debug for LuaFunction<L>
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<'lua, L> AsLua<'lua> for LuaFunction<L> where
L: AsLua<'lua>,
[src]
L: AsLua<'lua>,
fn as_lua(&self) -> LuaContext
[src]
impl<'lua, L> AsMutLua<'lua> for LuaFunction<L> where
L: AsMutLua<'lua>,
[src]
L: AsMutLua<'lua>,
fn as_mut_lua(&mut self) -> LuaContext
[src]
Returns the raw Lua context.
impl<'lua, L> LuaRead<L> for LuaFunction<L> where
L: AsMutLua<'lua>,
[src]
L: AsMutLua<'lua>,
fn lua_read_at_position(lua: L, index: i32) -> Result<LuaFunction<L>, L>
[src]
Reads the data from Lua at a given position.
fn lua_read(lua: L) -> Result<Self, L>
[src]
Reads the data from Lua.
Auto Trait Implementations
impl<L> Send for LuaFunction<L> where
L: Send,
L: Send,
impl<L> Sync for LuaFunction<L> where
L: Sync,
L: Sync,