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,

source

pub fn into_inner(self) -> L

source§

impl<'lua, L> LuaFunction<L>
where L: 'lua + AsLua,

source

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.

source

pub fn into_call<V>(self) -> Result<V, LuaError>
where V: LuaRead<PushGuard<Self>>,

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.

source

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

pub fn into_call_with_args<V, A>(self, args: A) -> Result<V, CallError<A::Err>>
where A: PushInto<LuaState>, V: LuaRead<PushGuard<Self>>,

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,

source

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

pub fn load(lua: L, code: &str) -> Result<Self, LuaError>

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§

source§

impl<L> AsLua for LuaFunction<L>
where L: AsLua,

source§

fn as_lua(&self) -> LuaState

source§

fn try_push<T>( self, v: T ) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>
where Self: Sized, T: PushInto<Self>,

Try to push v onto the lua stack. Read more
source§

fn push<T>(self, v: T) -> PushGuard<Self>
where Self: Sized, T: PushInto<Self>, <T as PushInto<Self>>::Err: Into<Void>,

Push v onto the lua stack. Read more
source§

fn try_push_one<T>( self, v: T ) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>
where Self: Sized, T: PushOneInto<Self>,

Try to push v onto the lua stack. Read more
source§

fn push_one<T>(self, v: T) -> PushGuard<Self>
where Self: Sized, T: PushOneInto<Self>, <T as PushInto<Self>>::Err: Into<Void>,

Push v onto the lua stack. Read more
source§

fn push_iter<I>(self, iterator: I) -> Result<PushGuard<Self>, Self>
where Self: Sized, I: Iterator, <I as Iterator>::Item: PushInto<LuaState>, <<I as Iterator>::Item as PushInto<LuaState>>::Err: Into<Void>,

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

fn try_push_iter<I>( self, iterator: I ) -> Result<PushGuard<Self>, (PushIterErrorOf<I>, Self)>
where Self: Sized, I: Iterator, <I as Iterator>::Item: PushInto<LuaState>,

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

fn read<T>(self) -> ReadResult<T, Self>
where Self: Sized, T: LuaRead<Self>,

source§

fn read_at<T>(self, index: i32) -> ReadResult<T, Self>
where Self: Sized, T: LuaRead<Self>,

source§

fn read_at_nz<T>(self, index: NonZeroI32) -> ReadResult<T, Self>
where Self: Sized, T: LuaRead<Self>,

source§

fn pcall<F, R>(&self, f: F) -> Result<R, LuaError>
where F: FnOnce(StaticLua) -> R,

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
source§

impl<L> AsRef<Object<L>> for LuaFunction<L>
where L: AsLua,

source§

fn as_ref(&self) -> &Object<L>

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

impl<L> Call<L> for LuaFunction<L>
where L: AsLua,

source§

fn call<'lua, R>(&'lua self) -> Result<R, LuaError>
where L: 'lua, R: LuaRead<PushGuard<&'lua L>>,

source§

fn call_with<'lua, A, R>(&'lua self, args: A) -> Result<R, CallError<A::Err>>
where L: 'lua, A: PushInto<LuaState>, R: LuaRead<PushGuard<&'lua L>>,

source§

fn into_call<R>(self) -> Result<R, LuaError>
where Self: AsLua + Sized, R: LuaRead<PushGuard<Self>>,

source§

fn into_call_with<A, R>(self, args: A) -> Result<R, CallError<A::Err>>
where Self: AsLua + Sized, A: PushInto<LuaState>, R: LuaRead<PushGuard<Self>>,

source§

impl<L: Debug> Debug for LuaFunction<L>

source§

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

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

impl<L> From<LuaFunction<L>> for Object<L>
where L: AsLua,

source§

fn from(o: LuaFunction<L>) -> Self

Converts to this type from the input type.
source§

impl<L> LuaRead<L> for LuaFunction<L>
where L: AsLua,

source§

fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>

Reads the data from Lua at a given position.
source§

fn n_values_expected() -> i32

source§

fn lua_read(lua: L) -> ReadResult<Self, L>

Reads the data from Lua.
source§

fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>
where L: AsLua,

source§

impl<L, T> Push<L> for LuaFunction<T>
where L: AsLua, T: AsLua,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(&self, lua: L) -> PushResult<L, Self>

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

fn push_no_err(&self, lua: L) -> PushGuard<L>
where <Self as Push<L>>::Err: Into<Void>,

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

impl<L> TryFrom<Object<L>> for LuaFunction<L>
where L: AsLua,

§

type Error = Object<L>

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

fn try_from(o: Object<L>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<L, T> PushOne<L> for LuaFunction<T>
where L: AsLua, T: AsLua,

Auto Trait Implementations§

§

impl<L> RefUnwindSafe for LuaFunction<L>
where L: RefUnwindSafe,

§

impl<L> Send for LuaFunction<L>
where L: Send,

§

impl<L> Sync for LuaFunction<L>
where L: Sync,

§

impl<L> Unpin for LuaFunction<L>
where L: Unpin,

§

impl<L> UnwindSafe for LuaFunction<L>
where L: 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.