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

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.

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.

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.

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));

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.

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));

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);

Builds a new LuaFunction from a raw string.

Note: This is just a wrapper around load_from_reader. There is no advantage in using load except that it is more convenient.

Trait Implementations

Try to push v onto the lua stack. Read more

Push v onto the lua stack. Read more

Try to push v onto the lua stack. Read more

Push v onto the lua stack. Read more

Push iterator onto the lua stack as a lua table. Read more

Push iterator onto the lua stack as a lua table. Read more

Call a rust function in protected mode. If a lua error is thrown during execution of f the function will return a LuaError. Read more

Converts this type into a shared reference of the (usually inferred) input type.

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Reads the data from Lua at a given position.

Reads the data from Lua.

Error that can happen when pushing a value.

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

Same as push_to_lua but can only succeed and is only available if Err implements Into<Void>. Read more

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.