Struct hlua::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 mut lua = hlua::Lua::new();
lua.execute::<()>("function foo() return 12 end").unwrap();

let mut foo: hlua::LuaFunction<_> = lua.get("foo").unwrap();
let result: i32 = foo.call().unwrap();
assert_eq!(result, 12);

Implementations§

source§

impl<'lua, L> LuaFunction<L>
where L: AsMutLua<'lua>,

source

pub fn call<'a, V>(&'a mut 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 call_with_args<'a, V, A, E>( &'a mut self, args: A ) -> Result<V, LuaFunctionCallError<E>>
where A: for<'r> Push<&'r mut LuaFunction<L>, Err = E>, V: LuaRead<PushGuard<&'a mut L>>,

Calls the function with parameters.

TODO: should be eventually be renamed to call

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 mut lua = hlua::Lua::new();
lua.execute::<()>("function sub(a, b) return a - b end").unwrap();

let mut foo: hlua::LuaFunction<_> = lua.get("sub").unwrap();
let result: i32 = foo.call_with_args((18, 4)).unwrap();
assert_eq!(result, 14);
source

pub fn load_from_reader<R>( lua: L, code: R ) -> Result<LuaFunction<PushGuard<L>>, LuaError>
where R: Read,

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 mut lua = hlua::Lua::new();

let mut f = hlua::LuaFunction::load_from_reader(&mut 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<LuaFunction<PushGuard<L>>, 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<'lua, L> AsLua<'lua> for LuaFunction<L>
where L: AsLua<'lua>,

source§

impl<'lua, L> AsMutLua<'lua> for LuaFunction<L>
where L: AsMutLua<'lua>,

source§

fn as_mut_lua(&mut self) -> LuaContext

Returns the raw Lua context.
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<'lua, L> LuaRead<L> for LuaFunction<L>
where L: AsMutLua<'lua>,

source§

fn lua_read_at_position(lua: L, index: i32) -> Result<LuaFunction<L>, L>

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

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

Reads the data from Lua.

Auto Trait Implementations§

§

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

§

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.