Struct hlua::Function
[−]
[src]
pub struct Function<F, P, R> { /* fields omitted */ }
Opaque type containing a Rust function or closure.
In order to build an instance of this struct, you need to use one of the functionN
functions.
There is one function for each possible number of parameter. For example if you have a function
with two parameters, you must use function2
.
Example:
let f: hlua::Function<_, _, _> = hlua::function2(move |a: i32, b: i32| { });
Note: In practice you will never need to build an object of type
Function
as an intermediary step. Instead you will most likely always immediately push the function, like in the code below.
You can push a Function
object like any other value:
use hlua::Lua; let mut lua = Lua::new(); lua.set("foo", hlua::function1(move |a: i32| -> i32 { a * 5 }));
The function can then be called from Lua:
lua.execute::<()>("a = foo(12)").unwrap(); assert_eq!(lua.get::<i32, _>("a").unwrap(), 60);
Remember that in Lua functions are regular variables, so you can do something like this for example:
lua.execute::<()>("bar = foo; a = bar(12)").unwrap();
Multiple return values
The Lua language supports functions that return multiple values at once.
In order to return multiple values from a Rust function, you can return a tuple. The elements of the tuple will be returned in order.
use hlua::Lua; let mut lua = Lua::new(); lua.set("values", hlua::function0(move || -> (i32, i32, i32) { (12, 24, 48) })); lua.execute::<()>("a, b, c = values()").unwrap(); assert_eq!(lua.get::<i32, _>("a").unwrap(), 12); assert_eq!(lua.get::<i32, _>("b").unwrap(), 24); assert_eq!(lua.get::<i32, _>("c").unwrap(), 48);
Using Result
If you want to return an error to the Lua script, you can use a Result
that contains an
Err
. The error will be returned to Lua as two values: A nil
value and the error message.
The error type of the Result
must implement the Display
trait, and will be turned into a
Lua string.
use hlua::Lua; let mut lua = Lua::new(); lua.openlibs(); lua.set("err", hlua::function0(move || -> Result<i32, &'static str> { Err("something wrong happened") })); lua.execute::<()>(r#" res, err = err(); assert(res == nil); assert(err == "something wrong happened"); "#).unwrap();
This also allows easy use of assert
to act like .unwrap()
in Rust:
use hlua::Lua; let mut lua = Lua::new(); lua.openlibs(); lua.set("err", hlua::function0(move || -> Result<i32, &'static str> { Err("something wrong happened") })); let ret = lua.execute::<()>("res = assert(err())"); assert!(ret.is_err());
Trait Implementations
impl<'lua, L, Z, R> Push<L> for Function<Z, (), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut() -> R,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut() -> R,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R> PushOne<L> for Function<Z, (), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut() -> R,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut() -> R,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static> Push<L> for Function<Z, (A,), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A) -> R,
(A,): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A) -> R,
(A,): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static> PushOne<L> for Function<Z, (A,), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A) -> R,
(A,): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A) -> R,
(A,): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static, B: 'static> Push<L> for Function<Z, (A, B), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B) -> R,
(A, B): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B) -> R,
(A, B): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static, B: 'static> PushOne<L> for Function<Z, (A, B), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B) -> R,
(A, B): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B) -> R,
(A, B): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static> Push<L> for Function<Z, (A, B, C), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C) -> R,
(A, B, C): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C) -> R,
(A, B, C): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static> PushOne<L> for Function<Z, (A, B, C), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C) -> R,
(A, B, C): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C) -> R,
(A, B, C): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static> Push<L> for Function<Z, (A, B, C, D), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D) -> R,
(A, B, C, D): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D) -> R,
(A, B, C, D): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static> PushOne<L> for Function<Z, (A, B, C, D), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D) -> R,
(A, B, C, D): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D) -> R,
(A, B, C, D): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static> Push<L> for Function<Z, (A, B, C, D, E), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E) -> R,
(A, B, C, D, E): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E) -> R,
(A, B, C, D, E): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static> PushOne<L> for Function<Z, (A, B, C, D, E), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E) -> R,
(A, B, C, D, E): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E) -> R,
(A, B, C, D, E): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static> Push<L> for Function<Z, (A, B, C, D, E, F), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F) -> R,
(A, B, C, D, E, F): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F) -> R,
(A, B, C, D, E, F): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static> PushOne<L> for Function<Z, (A, B, C, D, E, F), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F) -> R,
(A, B, C, D, E, F): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F) -> R,
(A, B, C, D, E, F): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static> Push<L> for Function<Z, (A, B, C, D, E, F, G), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G) -> R,
(A, B, C, D, E, F, G): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G) -> R,
(A, B, C, D, E, F, G): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static> PushOne<L> for Function<Z, (A, B, C, D, E, F, G), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G) -> R,
(A, B, C, D, E, F, G): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G) -> R,
(A, B, C, D, E, F, G): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static> Push<L> for Function<Z, (A, B, C, D, E, F, G, H), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H) -> R,
(A, B, C, D, E, F, G, H): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H) -> R,
(A, B, C, D, E, F, G, H): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static> PushOne<L> for Function<Z, (A, B, C, D, E, F, G, H), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H) -> R,
(A, B, C, D, E, F, G, H): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H) -> R,
(A, B, C, D, E, F, G, H): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static, I: 'static> Push<L> for Function<Z, (A, B, C, D, E, F, G, H, I), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H, I) -> R,
(A, B, C, D, E, F, G, H, I): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H, I) -> R,
(A, B, C, D, E, F, G, H, I): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static, I: 'static> PushOne<L> for Function<Z, (A, B, C, D, E, F, G, H, I), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H, I) -> R,
(A, B, C, D, E, F, G, H, I): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H, I) -> R,
(A, B, C, D, E, F, G, H, I): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static, I: 'static, J: 'static> Push<L> for Function<Z, (A, B, C, D, E, F, G, H, I, J), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H, I, J) -> R,
(A, B, C, D, E, F, G, H, I, J): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H, I, J) -> R,
(A, B, C, D, E, F, G, H, I, J): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
type Err = Void
Error that can happen when pushing a value.
fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>
Pushes the value on the top of the stack. Read more
fn push_no_err<E>(self, lua: L) -> PushGuard<L> where
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Self: Sized,
Self: Push<L, Err = E>,
E: Into<Void>,
Same as push_to_lua
but can only succeed and is only available if Err
is Void
.
impl<'lua, L, Z, R, A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static, I: 'static, J: 'static> PushOne<L> for Function<Z, (A, B, C, D, E, F, G, H, I, J), R> where
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H, I, J) -> R,
(A, B, C, D, E, F, G, H, I, J): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,
[src]
L: AsMutLua<'lua>,
Z: 'lua + FnMut(A, B, C, D, E, F, G, H, I, J) -> R,
(A, B, C, D, E, F, G, H, I, J): for<'p> LuaRead<&'p mut InsideCallback>,
R: for<'a> Push<&'a mut InsideCallback> + 'static,