Struct Function

Source
pub struct Function(/* private fields */);
Expand description

Handle to an internal Lua function.

Implementations§

Source§

impl Function

Source

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

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(&self, args: impl IntoLuaMulti) -> Result<Function>

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 deep_clone(&self) -> Self

Available on crate feature luau only.

Creates a deep clone of the Lua function.

Copies the function prototype and all its upvalues to the newly created function. This function returns shallow clone (same handle) for Rust/C functions.

Requires feature = "luau"

Source§

impl Function

Source

pub fn wrap<F, A, R>(func: F) -> impl IntoLua
where F: LuaNativeFn<A, Output = Result<R>> + MaybeSend + 'static, A: FromLuaMulti, R: IntoLuaMulti,

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

Source

pub fn wrap_mut<F, A, R>(func: F) -> impl IntoLua
where F: LuaNativeFnMut<A, Output = Result<R>> + MaybeSend + 'static, A: FromLuaMulti, R: IntoLuaMulti,

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

Source

pub fn wrap_raw<F, A>(func: F) -> impl IntoLua
where F: LuaNativeFn<A> + MaybeSend + 'static, A: FromLuaMulti,

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

This function is similar to Function::wrap but any returned Result will be converted to a ok, err tuple without throwing an exception.

Source

pub fn wrap_raw_mut<F, A>(func: F) -> impl IntoLua
where F: LuaNativeFnMut<A> + MaybeSend + 'static, A: FromLuaMulti,

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

This function is similar to Function::wrap_mut but any returned Result will be converted to a ok, err tuple without throwing an exception.

Source

pub fn wrap_async<F, A, R>(func: F) -> impl IntoLua
where F: LuaNativeAsyncFn<A, Output = Result<R>> + MaybeSend + 'static, A: FromLuaMulti, R: IntoLuaMulti,

Available on crate feature async only.

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

Source

pub fn wrap_raw_async<F, A>(func: F) -> impl IntoLua
where F: LuaNativeAsyncFn<A> + MaybeSend + 'static, A: FromLuaMulti,

Available on crate feature async only.

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

This function is similar to Function::wrap_async but any returned Result will be converted to a ok, err tuple without throwing an exception.

Trait Implementations§

Source§

impl Clone for Function

Source§

fn clone(&self) -> Function

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 Debug for Function

Source§

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

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

impl FromLua for Function

Source§

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

Performs the conversion.
Source§

impl IntoLua for &Function

Source§

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

Performs the conversion.
Source§

impl IntoLua for Function

Source§

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

Performs the conversion.
Source§

impl PartialEq for Function

Source§

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

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

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 StructuralPartialEq for Function

Auto Trait Implementations§

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<T> FromLuaMulti for T
where T: FromLua,

Source§

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

Performs the conversion. Read more
Source§

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

Source§

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

Source§

unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &RawLua, ) -> 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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoLuaMulti for T
where T: IntoLua,

Source§

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

Performs the conversion.
Source§

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

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

impl<T> MaybeSend for T
where T: Send,