Struct mlua::Function

source ·
pub struct Function<'lua>(/* private fields */);
Expand description

Handle to an internal Lua function.

Implementations§

source§

impl<'lua> Function<'lua>

source

pub fn call<A: IntoLuaMulti<'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.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<A, R>( &self, args: A ) -> impl Future<Output = Result<R>> + 'lua
where A: IntoLuaMulti<'lua>, R: FromLuaMulti<'lua> + 'lua,

Available on crate feature async only.

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;

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

sleep.call_async(10).await?;
source

pub fn bind<A: IntoLuaMulti<'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.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 environment(&self) -> Option<Table<'_>>

Returns the environment of the Lua function.

By default Lua functions shares a global environment.

This function always returns None for Rust/C functions.

source

pub fn set_environment(&self, env: Table<'_>) -> Result<bool>

Sets the environment of the Lua function.

The environment is a table that is used as the global environment for the function. Returns true if environment successfully changed, false otherwise.

This function does nothing for Rust/C functions.

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>

Available on non-crate feature luau only.

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.

source

pub fn coverage<F>(&self, func: F)
where F: FnMut(CoverageInfo),

Available on crate feature luau only.

Retrieves recorded coverage information about this Lua function including inner calls.

This function takes a callback as an argument and calls it providing CoverageInfo snapshot per each executed inner function.

Recording of coverage information is controlled by Compiler::set_coverage_level option.

Requires feature = "luau"

source

pub fn to_pointer(&self) -> *const c_void

Converts this function to a generic C pointer.

There is no way to convert the pointer back to its original value.

Typically this function is used only for hashing and debug information.

source

pub fn into_owned(self) -> OwnedFunction

Available on crate feature unstable and non-crate feature send only.

Convert this handle to owned version.

source§

impl<'lua> Function<'lua>

source

pub fn wrap<A, R, F>(func: F) -> impl IntoLua<'lua>
where A: FromLuaMulti<'lua>, R: IntoLuaMulti<'lua>, F: Fn(&'lua Lua, A) -> Result<R> + MaybeSend + 'static,

Wraps a Rust function or closure, returning an opaque type that implements IntoLua trait.

source

pub fn wrap_mut<A, R, F>(func: F) -> impl IntoLua<'lua>
where A: FromLuaMulti<'lua>, R: IntoLuaMulti<'lua>, F: FnMut(&'lua Lua, A) -> Result<R> + MaybeSend + 'static,

Wraps a Rust mutable closure, returning an opaque type that implements IntoLua trait.

source

pub fn wrap_async<A, R, F, FR>(func: F) -> impl IntoLua<'lua>
where A: FromLuaMulti<'lua>, R: IntoLuaMulti<'lua>, F: Fn(&'lua Lua, A) -> FR + MaybeSend + 'static, FR: Future<Output = Result<R>> + 'lua,

Available on crate feature async only.

Wraps a Rust async function or closure, returning an opaque type that implements IntoLua trait.

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>, _: &'lua Lua) -> Result<Function<'lua>>

Performs the conversion.
source§

impl<'lua> IntoLua<'lua> for &Function<'lua>

source§

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

Performs the conversion.
source§

impl<'lua> IntoLua<'lua> for Function<'lua>

source§

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

Performs the conversion.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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

fn from_lua_args( args: MultiValue<'lua>, i: usize, to: Option<&str>, lua: &'lua Lua ) -> Result<T, Error>

source§

unsafe fn from_stack_multi(nvals: i32, lua: &'lua Lua) -> Result<T, Error>

source§

unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &'lua Lua ) -> Result<T, Error>

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> IntoLuaMulti<'lua> for T
where T: IntoLua<'lua>,

source§

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

Performs the conversion.
source§

unsafe fn push_into_stack_multi(self, lua: &'lua Lua) -> Result<i32, Error>

source§

impl<T> ToOwned for T
where 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 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.