Add Function::wrap/Function::wrap_mut/Function::wrap_async to wrap functions into a type that implements IntoLua trait.

This is useful to avoid calling `lua.create_function*` every time when `Function` handle is needed.
This commit is contained in:
Alex Orlenko
2022-12-22 16:24:35 +00:00
parent 9d28b790e7
commit 1d4a135e8e
4 changed files with 140 additions and 1 deletions
+24 -1
View File
@@ -19,7 +19,14 @@ use crate::userdata::{AnyUserData, UserData};
use crate::value::{FromLua, IntoLua, Nil, Value};
#[cfg(feature = "unstable")]
use crate::{function::OwnedFunction, table::OwnedTable, userdata::OwnedAnyUserData};
use crate::{
function::{OwnedFunction, WrappedFunction},
table::OwnedTable,
userdata::OwnedAnyUserData,
};
#[cfg(all(feature = "async", feature = "unstable"))]
use crate::function::WrappedAsyncFunction;
impl<'lua> IntoLua<'lua> for Value<'lua> {
#[inline]
@@ -129,6 +136,22 @@ impl<'lua> FromLua<'lua> for OwnedFunction {
}
}
#[cfg(feature = "unstable")]
impl<'lua> IntoLua<'lua> for WrappedFunction<'lua> {
#[inline]
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
lua.create_callback(self.0).map(Value::Function)
}
}
#[cfg(all(feature = "async", feature = "unstable"))]
impl<'lua> IntoLua<'lua> for WrappedAsyncFunction<'lua> {
#[inline]
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
lua.create_async_callback(self.0).map(Value::Function)
}
}
impl<'lua> IntoLua<'lua> for Thread<'lua> {
#[inline]
fn into_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {