Struct LuaFunction

Source
pub struct LuaFunction { /* private fields */ }
Expand description

Reperesents a callable function on the Lua stack

Functions that can be called may be defined in Lua, Rust, C, or any other language with Lua bindings.

§Examples

context.do_string("function double(x) return x * 2 end").unwrap();
let lua_double: LuaFunction = context.push_global("double")
    .get_value(&mut context).unwrap();
let result = lua_double.call_singleret(&mut context, &[&4])
    .and_then(|v| v.get_value(&mut context));
assert_eq!(result, Some(8));

Implementations§

Source§

impl LuaFunction

Source

pub fn new(i: Index) -> LuaFunction

Create a new LuaFunction given an index

Source

pub fn pcall( &self, context: &mut Context<'_>, args: &[&dyn ToLua], errfunc: Option<&LuaFunction>, nresults: i32, ) -> Result<Vec<LuaGeneric>>

Protected function call, errors return an Err If the function call is successful, returns a list of all return values (with a maximum size nresults)

§Errors

Returns an Error if Lua encounters a runtime error while running this function. If an errfunc is given, then the given function will be called with the error message as an argument, and the function should return a new error message. errfunc will not be called if no error was encountered, the error that occured was an out of memeory error, or if another error occurred while running errfunc.

Source

pub fn pcall_multiret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], errfunc: Option<&LuaFunction>, ) -> Result<Vec<LuaGeneric>>

Same as pcall, but returns all return values

Source

pub fn pcall_singleret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], errfunc: Option<&LuaFunction>, ) -> Result<Option<LuaGeneric>>

Same as pcall, but only returns at most one return value.

Source

pub fn pcall_noret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], errfunc: Option<&LuaFunction>, ) -> Result<()>

Same as pcall, but returns nothing.

Source

pub fn call( &self, context: &mut Context<'_>, args: &[&dyn ToLua], nresults: i32, ) -> Vec<LuaGeneric>

Call this function, returns a list of return values (with a maximum size nresults).

§Panics

This function will panic if Lua encounters a runtime error. It’s not really a panic, it’s actually a longjmp, but it might as well be a panic.

Source

pub fn call_singleret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], ) -> Option<LuaGeneric>

Same as call, but returns at most one return value.

Source

pub fn call_multiret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], ) -> Vec<LuaGeneric>

Same as call, but returns all return values.

Source

pub fn call_noret(&self, context: &mut Context<'_>, args: &[&dyn ToLua])

Same as call, but does not return any return values.

Trait Implementations§

Source§

impl FromLua for LuaFunction

Source§

fn from_lua(state: &mut State, index: Index) -> Option<LuaFunction>

Converts the value on top of the stack of a Lua state to a value of type Option<Self>.
Source§

impl LuaStackable for LuaFunction

Source§

fn get_pos(&self) -> Index

Get the position of this value on the stack
Source§

impl ToLua for LuaFunction

Source§

fn to_lua(&self, state: &mut State)

Pushes a value of type Self onto the stack of a Lua state.

Auto Trait Implementations§

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>,

Source§

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>,

Source§

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.