Struct mlua::prelude::LuaFunction[][src]

pub struct LuaFunction<'lua>(_);
Expand description

Handle to an internal Lua function.

Implementations

impl<'lua> Function<'lua>[src]

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

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

pub fn call_async<'fut, A, R>(&self, args: A) -> LocalBoxFuture<'fut, Result<R>> where
    'lua: 'fut,
    A: ToLuaMulti<'lua>,
    R: FromLuaMulti<'lua> + 'fut, 
[src]

This is supported on crate feature async only.

Returns a Feature 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?;

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

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

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

Notable traits for Vec<u8, A>

impl<A> Write for Vec<u8, A> where
    A: Allocator
[src]

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.

Trait Implementations

impl<'lua> Clone for Function<'lua>[src]

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

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<'lua> Debug for Function<'lua>[src]

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

Formats the value using the given formatter. Read more

impl<'lua> FromLua<'lua> for Function<'lua>[src]

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

Performs the conversion.

impl<'lua> PartialEq<Function<'lua>> for Function<'lua>[src]

fn eq(&self, other: &Self) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl<'lua> ToLua<'lua> for Function<'lua>[src]

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

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.