Struct rlua::Function

source ·
pub struct Function<'lua>(_);
Expand description

Handle to an internal Lua function.

Implementations§

source§

impl<'lua> Function<'lua>

source

pub fn call<A: ToLuaMulti<'lua>, R: FromLuaMulti<'lua>>( &self, args: A ) -> Result<R>

Calls the function, passing args as function arguments.

The function’s return values are converted to the generic type R.

Examples

Call Lua’s built-in tostring function:

let globals = lua_context.globals();

let tostring: Function = globals.get("tostring")?;

assert_eq!(tostring.call::<_, String>(123)?, "123");

Call a function with multiple arguments:

let sum: Function = lua_context.load(
    r#"
        function(a, b)
            return a + b
        end
    "#).eval()?;

assert_eq!(sum.call::<_, u32>((3, 4))?, 3 + 4);
source

pub fn bind<A: ToLuaMulti<'lua>>(&self, args: A) -> Result<Function<'lua>>

Returns a function that, when called, calls self, passing args as the first set of arguments.

If any arguments are passed to the returned function, they will be passed after args.

Examples
let sum: Function = lua_context.load(r#"
    function(a, b)
        return a + b
    end
"#).eval()?;

let bound_a = sum.bind(1)?;
assert_eq!(bound_a.call::<_, u32>(2)?, 1 + 2);

let bound_a_and_b = sum.bind(13)?.bind(57)?;
assert_eq!(bound_a_and_b.call::<_, u32>(())?, 13 + 57);
source

pub fn dump(&self) -> Result<Vec<u8>>

Dumps the compiled representation of the function into a binary blob, which can later be loaded using the unsafe Chunk::into_function_allow_binary().

Examples
let add2: Function = lua_context.load(r#"
    function(a)
        return a + 2
    end
"#).eval()?;

let dumped = add2.dump()?;

let reloaded = unsafe {
    lua_context.load(&dumped)
               .into_function_allow_binary()?
};
assert_eq!(reloaded.call::<_, u32>(7)?, 7+2);

Trait Implementations§

source§

impl<'lua> Clone for Function<'lua>

source§

fn clone(&self) -> Function<'lua>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'lua> Debug for Function<'lua>

source§

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

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

impl<'lua> FromLua<'lua> for Function<'lua>

source§

fn from_lua(value: Value<'lua>, _: Context<'lua>) -> Result<Function<'lua>>

Performs the conversion.
source§

impl<'lua> ToLua<'lua> for Function<'lua>

source§

fn to_lua(self, _: Context<'lua>) -> Result<Value<'lua>>

Performs the conversion.

Auto Trait Implementations§

§

impl<'lua> !RefUnwindSafe for Function<'lua>

§

impl<'lua> !Send for Function<'lua>

§

impl<'lua> !Sync for Function<'lua>

§

impl<'lua> Unpin for Function<'lua>

§

impl<'lua> !UnwindSafe for Function<'lua>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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<'lua, T> FromLuaMulti<'lua> for Twhere T: FromLua<'lua>,

source§

fn from_lua_multi( values: MultiValue<'lua>, lua: Context<'lua> ) -> Result<T, Error>

Performs the conversion. Read more
source§

impl<T, U> Into<U> for Twhere 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<'lua, T> ToLuaMulti<'lua> for Twhere T: ToLua<'lua>,

source§

fn to_lua_multi(self, lua: Context<'lua>) -> Result<MultiValue<'lua>, Error>

Performs the conversion.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.