Minor refactor
This commit is contained in:
parent
ee08050c1f
commit
c826798a6d
|
@ -10,7 +10,7 @@ use crate::util::{
|
||||||
use crate::value::{FromLuaMulti, MultiValue, ToLuaMulti};
|
use crate::value::{FromLuaMulti, MultiValue, ToLuaMulti};
|
||||||
|
|
||||||
#[cfg(feature = "async")]
|
#[cfg(feature = "async")]
|
||||||
use futures_core::future::LocalBoxFuture;
|
use {futures_core::future::LocalBoxFuture, futures_util::future};
|
||||||
|
|
||||||
/// Handle to an internal Lua function.
|
/// Handle to an internal Lua function.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -123,7 +123,7 @@ impl<'lua> Function<'lua> {
|
||||||
let lua = self.0.lua;
|
let lua = self.0.lua;
|
||||||
match lua.create_thread(self.clone()) {
|
match lua.create_thread(self.clone()) {
|
||||||
Ok(t) => Box::pin(t.into_async(args)),
|
Ok(t) => Box::pin(t.into_async(args)),
|
||||||
Err(e) => Box::pin(futures_util::future::err(e)),
|
Err(e) => Box::pin(future::err(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
39
src/lua.rs
39
src/lua.rs
|
@ -27,13 +27,12 @@ use crate::value::{FromLua, FromLuaMulti, MultiValue, Nil, ToLua, ToLuaMulti, Va
|
||||||
#[cfg(feature = "async")]
|
#[cfg(feature = "async")]
|
||||||
use {
|
use {
|
||||||
crate::types::AsyncCallback,
|
crate::types::AsyncCallback,
|
||||||
futures_core::future::LocalBoxFuture,
|
futures_core::{
|
||||||
futures_task::noop_waker,
|
future::{Future, LocalBoxFuture},
|
||||||
futures_util::future::{self, FutureExt, TryFutureExt},
|
|
||||||
std::{
|
|
||||||
future::Future,
|
|
||||||
task::{Context, Poll, Waker},
|
task::{Context, Poll, Waker},
|
||||||
},
|
},
|
||||||
|
futures_task::noop_waker,
|
||||||
|
futures_util::future::{self, TryFutureExt},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Top level Lua struct which holds the Lua state itself.
|
/// Top level Lua struct which holds the Lua state itself.
|
||||||
|
@ -530,12 +529,10 @@ impl Lua {
|
||||||
{
|
{
|
||||||
self.create_async_callback(Box::new(move |lua, args| {
|
self.create_async_callback(Box::new(move |lua, args| {
|
||||||
let args = match A::from_lua_multi(args, lua) {
|
let args = match A::from_lua_multi(args, lua) {
|
||||||
Ok(x) => x,
|
Ok(args) => args,
|
||||||
Err(e) => return future::err(e).boxed_local(),
|
Err(e) => return Box::pin(future::err(e)),
|
||||||
};
|
};
|
||||||
func(lua, args)
|
Box::pin(func(lua, args).and_then(move |ret| future::ready(ret.to_lua_multi(lua))))
|
||||||
.and_then(move |x| future::ready(x.to_lua_multi(lua)))
|
|
||||||
.boxed_local()
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1420,8 +1417,8 @@ impl<'lua, 'a> Chunk<'lua, 'a> {
|
||||||
R: FromLuaMulti<'lua> + 'fut,
|
R: FromLuaMulti<'lua> + 'fut,
|
||||||
{
|
{
|
||||||
match self.into_function() {
|
match self.into_function() {
|
||||||
Ok(f) => f.call_async(args),
|
Ok(func) => func.call_async(args),
|
||||||
Err(e) => Box::pin(futures_util::future::err(e)),
|
Err(e) => Box::pin(future::err(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1742,7 +1739,7 @@ impl<'lua, T: 'static + UserData> StaticUserDataMethods<'lua, T> {
|
||||||
MR: 'static + Future<Output = Result<R>>,
|
MR: 'static + Future<Output = Result<R>>,
|
||||||
{
|
{
|
||||||
Box::new(move |lua, mut args| {
|
Box::new(move |lua, mut args| {
|
||||||
let fut = || {
|
let fut_res = || {
|
||||||
if let Some(front) = args.pop_front() {
|
if let Some(front) = args.pop_front() {
|
||||||
let userdata = AnyUserData::from_lua(front, lua)?;
|
let userdata = AnyUserData::from_lua(front, lua)?;
|
||||||
let userdata = userdata.borrow::<T>()?.clone();
|
let userdata = userdata.borrow::<T>()?.clone();
|
||||||
|
@ -1755,11 +1752,9 @@ impl<'lua, T: 'static + UserData> StaticUserDataMethods<'lua, T> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match fut() {
|
match fut_res() {
|
||||||
Ok(f) => f
|
Ok(fut) => Box::pin(fut.and_then(move |ret| future::ready(ret.to_lua_multi(lua)))),
|
||||||
.and_then(move |fr| future::ready(fr.to_lua_multi(lua)))
|
Err(e) => Box::pin(future::err(e)),
|
||||||
.boxed_local(),
|
|
||||||
Err(e) => future::err(e).boxed_local(),
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1798,12 +1793,10 @@ impl<'lua, T: 'static + UserData> StaticUserDataMethods<'lua, T> {
|
||||||
{
|
{
|
||||||
Box::new(move |lua, args| {
|
Box::new(move |lua, args| {
|
||||||
let args = match A::from_lua_multi(args, lua) {
|
let args = match A::from_lua_multi(args, lua) {
|
||||||
Ok(x) => x,
|
Ok(args) => args,
|
||||||
Err(e) => return future::err(e).boxed_local(),
|
Err(e) => return Box::pin(future::err(e)),
|
||||||
};
|
};
|
||||||
function(lua, args)
|
Box::pin(function(lua, args).and_then(move |ret| future::ready(ret.to_lua_multi(lua))))
|
||||||
.and_then(move |x| future::ready(x.to_lua_multi(lua)))
|
|
||||||
.boxed_local()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
33
src/table.rs
33
src/table.rs
|
@ -9,10 +9,7 @@ use crate::util::{assert_stack, protect_lua, protect_lua_closure, StackGuard};
|
||||||
use crate::value::{FromLua, FromLuaMulti, Nil, ToLua, ToLuaMulti, Value};
|
use crate::value::{FromLua, FromLuaMulti, Nil, ToLua, ToLuaMulti, Value};
|
||||||
|
|
||||||
#[cfg(feature = "async")]
|
#[cfg(feature = "async")]
|
||||||
use {
|
use {futures_core::future::LocalBoxFuture, futures_util::future};
|
||||||
futures_core::future::LocalBoxFuture,
|
|
||||||
futures_util::future::{self, FutureExt},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Handle to an internal Lua table.
|
/// Handle to an internal Lua table.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -145,23 +142,6 @@ impl<'lua> Table<'lua> {
|
||||||
/// This function is deprecated since 0.3.1 in favor of [`call_method`]
|
/// This function is deprecated since 0.3.1 in favor of [`call_method`]
|
||||||
/// in the `TableExt` trait.
|
/// in the `TableExt` trait.
|
||||||
///
|
///
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// Execute the table method with name "concat":
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use mlua::{Lua, Result, Table};
|
|
||||||
/// # fn main() -> Result<()> {
|
|
||||||
/// # let lua = Lua::new();
|
|
||||||
/// # let object = lua.create_table()?;
|
|
||||||
/// # let concat = lua.create_function(|_, (_, a, b): (Table, String, String)| Ok(a + &b))?;
|
|
||||||
/// # object.set("concat", concat)?;
|
|
||||||
/// // simiar to: object:concat("param1", "param2")
|
|
||||||
/// object.call("concat", ("param1", "param2"))?;
|
|
||||||
/// # Ok(())
|
|
||||||
/// # }
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// This might invoke the `__index` metamethod.
|
/// This might invoke the `__index` metamethod.
|
||||||
///
|
///
|
||||||
/// [`call_method`]: trait.TableExt.html#tymethod.call_method
|
/// [`call_method`]: trait.TableExt.html#tymethod.call_method
|
||||||
|
@ -596,7 +576,7 @@ impl<'lua> TableExt<'lua> for Table<'lua> {
|
||||||
let lua = self.0.lua;
|
let lua = self.0.lua;
|
||||||
let mut args = match args.to_lua_multi(lua) {
|
let mut args = match args.to_lua_multi(lua) {
|
||||||
Ok(args) => args,
|
Ok(args) => args,
|
||||||
Err(e) => return future::err(e).boxed_local(),
|
Err(e) => return Box::pin(future::err(e)),
|
||||||
};
|
};
|
||||||
args.push_front(Value::Table(self.clone()));
|
args.push_front(Value::Table(self.clone()));
|
||||||
self.call_async_function(key, args)
|
self.call_async_function(key, args)
|
||||||
|
@ -610,11 +590,10 @@ impl<'lua> TableExt<'lua> for Table<'lua> {
|
||||||
A: ToLuaMulti<'lua>,
|
A: ToLuaMulti<'lua>,
|
||||||
R: FromLuaMulti<'lua> + 'fut,
|
R: FromLuaMulti<'lua> + 'fut,
|
||||||
{
|
{
|
||||||
let func = match self.get::<_, Function>(key) {
|
match self.get::<_, Function>(key) {
|
||||||
Ok(f) => f,
|
Ok(func) => func.call_async(args),
|
||||||
Err(e) => return future::err(e).boxed_local(),
|
Err(e) => Box::pin(future::err(e)),
|
||||||
};
|
}
|
||||||
func.call_async(args)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue