Struct hlua::Function

source ·
pub struct Function<F, P, R> { /* private fields */ }
Expand description

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§

source§

impl<F: Debug, P: Debug, R: Debug> Debug for Function<F, P, R>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(self, lua: L) -> Result<PushGuard<L>, (Void, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err<E>(self, lua: L) -> PushGuard<L>
where Self: Sized + Push<L, Err = E>, E: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err is Void.
source§

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,

source§

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,

source§

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,

source§

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,

source§

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,

source§

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,

source§

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,

source§

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,

source§

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,

source§

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,

source§

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,

Auto Trait Implementations§

§

impl<F, P, R> Freeze for Function<F, P, R>
where F: Freeze,

§

impl<F, P, R> RefUnwindSafe for Function<F, P, R>

§

impl<F, P, R> Send for Function<F, P, R>
where F: Send, P: Send, R: Send,

§

impl<F, P, R> Sync for Function<F, P, R>
where F: Sync, P: Sync, R: Sync,

§

impl<F, P, R> Unpin for Function<F, P, R>
where F: Unpin, P: Unpin, R: Unpin,

§

impl<F, P, R> UnwindSafe for Function<F, P, R>
where F: UnwindSafe, P: UnwindSafe, R: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.