Struct tlua::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 lua = tlua::Lua::new();
lua.exec("function foo() return 12 end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("foo").unwrap();
let result: i32 = foo.call().unwrap();
assert_eq!(result, 12);
Implementations§
source§impl<L> LuaFunction<L>where
L: AsLua,
impl<L> LuaFunction<L>where
L: AsLua,
pub fn into_inner(self) -> L
source§impl<'lua, L> LuaFunction<L>where
L: 'lua + AsLua,
impl<'lua, L> LuaFunction<L>where
L: 'lua + AsLua,
sourcepub fn call<V>(&'lua self) -> Result<V, LuaError>
pub fn call<V>(&'lua 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 into_call<V>(self) -> Result<V, LuaError>
pub fn into_call<V>(self) -> Result<V, LuaError>
Calls the function taking ownership of the underlying push guard. 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
into_call_with_args
instead.
sourcepub fn call_with_args<V, A>(&'lua self, args: A) -> Result<V, CallError<A::Err>>
pub fn call_with_args<V, A>(&'lua self, args: A) -> Result<V, CallError<A::Err>>
Calls the function with parameters.
TODO: should be eventually be renamed to call
Note: this function can return multiple values if V
is a tuple.
- If the expected number of values is less than the actual, only the first few values will be returned.
- If the expected number of values is greater than the actual, the
function will return an error, unless the excess elements are
Option<T>
.
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 lua = tlua::Lua::new();
lua.exec("function sub(a, b) return a - b end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("sub").unwrap();
let result: i32 = foo.call_with_args((18, 4)).unwrap();
assert_eq!(result, 14);
Multiple return values
let lua = tlua::Lua::new();
lua.exec("function divmod(a, b) return math.floor(a / b), a % b end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("divmod").unwrap();
let first_result: i32 = foo.call_with_args((18, 4)).unwrap();
assert_eq!(first_result, 4);
let all_result: (i32, i32) = foo.call_with_args((18, 4)).unwrap();
assert_eq!(all_result, (4, 2));
let excess_results: (i32, i32, Option<i32>) = foo.call_with_args((18, 4)).unwrap();
assert_eq!(excess_results, (4, 2, None));
sourcepub fn into_call_with_args<V, A>(self, args: A) -> Result<V, CallError<A::Err>>
pub fn into_call_with_args<V, A>(self, args: A) -> Result<V, CallError<A::Err>>
Calls the function with parameters taking ownership of the underlying push guard.
TODO: should be eventually be renamed to call
Note: this function can return multiple values if V
is a tuple.
- If the expected number of values is less than the actual, only the first few values will be returned.
- If the expected number of values is greater than the actual, the
function will return an error, unless the excess elements are
Option<T>
.
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 lua = tlua::Lua::new();
lua.exec("function sub(a, b) return a - b end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("sub").unwrap();
let result: i32 = foo.into_call_with_args((18, 4)).unwrap();
assert_eq!(result, 14);
Multiple return values
let lua = tlua::Lua::new();
lua.exec("function divmod(a, b) return math.floor(a / b), a % b end").unwrap();
let foo: tlua::LuaFunction<_> = lua.get("divmod").unwrap();
let all_result: (i32, i32) = foo.into_call_with_args((18, 4)).unwrap();
assert_eq!(all_result, (4, 2));
source§impl<L> LuaFunction<PushGuard<L>>where
L: AsLua,
impl<L> LuaFunction<PushGuard<L>>where
L: AsLua,
sourcepub fn load_from_reader(lua: L, code: impl Read) -> Result<Self, LuaError>
pub fn load_from_reader(lua: L, code: impl Read) -> Result<Self, LuaError>
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 lua = tlua::Lua::new();
let f = tlua::LuaFunction::load_from_reader(&lua, Cursor::new("return 8")).unwrap();
let ret: i32 = f.call().unwrap();
assert_eq!(ret, 8);
Trait Implementations§
source§impl<L> AsLua for LuaFunction<L>where
L: AsLua,
impl<L> AsLua for LuaFunction<L>where
L: AsLua,
fn as_lua(&self) -> LuaState
source§fn try_push<T>(
self,
v: T
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>
fn try_push<T>( self, v: T ) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>
v
onto the lua stack. Read moresource§fn try_push_one<T>(
self,
v: T
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>where
Self: Sized,
T: PushOneInto<Self>,
fn try_push_one<T>(
self,
v: T
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>where
Self: Sized,
T: PushOneInto<Self>,
v
onto the lua stack. Read moresource§fn push_iter<I>(self, iterator: I) -> Result<PushGuard<Self>, Self>
fn push_iter<I>(self, iterator: I) -> Result<PushGuard<Self>, Self>
iterator
onto the lua stack as a lua table. Read moresource§fn try_push_iter<I>(
self,
iterator: I
) -> Result<PushGuard<Self>, (PushIterErrorOf<I>, Self)>
fn try_push_iter<I>( self, iterator: I ) -> Result<PushGuard<Self>, (PushIterErrorOf<I>, Self)>
iterator
onto the lua stack as a lua table. Read more