Struct LuaFunction

Source
pub struct LuaFunction<'lua>(/* private fields */);
Expand description

Handle to an internal Lua function.

Implementations§

Source§

impl<'lua> Function<'lua>

Source

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

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.globals();

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

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

Call a function with multiple arguments:

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

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

pub fn call_async<'fut, A, R>( &self, args: A, ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + 'fut>>
where 'lua: 'fut, A: ToLuaMulti<'lua>, R: FromLuaMulti<'lua> + 'fut,

Returns a future that, when polled, calls self, passing args as function arguments, and drives the execution.

Internally it wraps the function to an AsyncThread.

Requires feature = "async"

§Examples
use std::time::Duration;
use futures_timer::Delay;

let sleep = lua.create_async_function(move |_lua, n: u64| async move {
    Delay::new(Duration::from_millis(n)).await;
    Ok(())
})?;

sleep.call_async(10).await?;
Source

pub fn bind<A>(&self, args: A) -> Result<Function<'lua>, Error>
where A: ToLuaMulti<'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.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 info(&self) -> FunctionInfo

Returns information about the function.

Corresponds to the >Sn what mask for lua_getinfo when applied to the function.

Source

pub fn dump(&self, strip: bool) -> Vec<u8>

Dumps the function as a binary chunk.

If strip is true, the binary representation may not include all debug information about the function, to save space.

For Luau a Compiler can be used to compile Lua chunks to bytecode.

Trait Implementations§

Source§

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

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

const 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<(), Error>

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

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

Source§

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

Performs the conversion.
Source§

impl<'lua> PartialEq for Function<'lua>

Source§

fn eq(&self, other: &Function<'lua>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>, Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'lua> Freeze for Function<'lua>

§

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 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where T: FromLua<'lua>,

Source§

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

Performs the conversion. Read more
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<'lua, T> ToLuaMulti<'lua> for T
where T: ToLua<'lua>,

Source§

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

Performs the conversion.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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