Some lifetime and async arg improvements

This commit is contained in:
Filip Tibell 2023-08-18 12:23:26 -05:00
parent 62e6130223
commit e1fa89cf60
2 changed files with 13 additions and 12 deletions

View file

@ -1,7 +1,7 @@
use futures_util::Future; use futures_util::Future;
use mlua::prelude::*; use mlua::prelude::*;
use super::Scheduler; use super::{IntoLuaOwnedThread, Scheduler};
impl<'lua, 'fut> Scheduler<'fut> impl<'lua, 'fut> Scheduler<'fut>
where where
@ -12,7 +12,7 @@ where
*/ */
pub fn schedule_future<F>(&'fut self, fut: F) pub fn schedule_future<F>(&'fut self, fut: F)
where where
F: 'fut + Future<Output = ()>, F: Future<Output = ()> + 'fut,
{ {
let futs = self let futs = self
.futures .futures
@ -26,13 +26,14 @@ where
*/ */
pub fn schedule_future_thread<F, FR>( pub fn schedule_future_thread<F, FR>(
&'fut self, &'fut self,
thread: LuaOwnedThread, thread: impl IntoLuaOwnedThread,
fut: F, fut: F,
) -> LuaResult<()> ) -> LuaResult<()>
where where
FR: IntoLuaMulti<'fut>, FR: IntoLuaMulti<'fut>,
F: 'fut + Future<Output = LuaResult<FR>>, F: Future<Output = LuaResult<FR>> + 'fut,
{ {
let thread = thread.into_owned_lua_thread(&self.lua)?;
self.schedule_future(async move { self.schedule_future(async move {
let rets = fut.await.expect("Failed to receive result"); let rets = fut.await.expect("Failed to receive result");
let rets = rets let rets = rets

View file

@ -25,8 +25,8 @@ where
where where
A: FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'lua>, R: IntoLuaMulti<'lua>,
F: 'static + Fn(&'lua Lua, A) -> FR, F: Fn(&'lua Lua, A) -> FR + 'static,
FR: 'static + Future<Output = LuaResult<R>>; FR: Future<Output = LuaResult<R>> + 'fut;
} }
impl<'lua, 'fut> LuaSchedulerExt<'lua, 'fut> for Lua impl<'lua, 'fut> LuaSchedulerExt<'lua, 'fut> for Lua
@ -37,8 +37,8 @@ where
where where
A: FromLuaMulti<'lua>, A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'lua>, R: IntoLuaMulti<'lua>,
F: 'static + Fn(&'lua Lua, A) -> FR, F: Fn(&'lua Lua, A) -> FR + 'static,
FR: 'static + Future<Output = LuaResult<R>>, FR: Future<Output = LuaResult<R>> + 'fut,
{ {
let async_env = self.create_table_with_capacity(0, 2)?; let async_env = self.create_table_with_capacity(0, 2)?;
@ -52,12 +52,12 @@ where
async_env.set( async_env.set(
"schedule", "schedule",
LuaFunction::wrap(move |lua: &Lua, args: A| { LuaFunction::wrap(move |lua: &Lua, args: A| {
let _thread = lua.current_thread().into_owned(); let thread = lua.current_thread();
let _future = func(lua, args); let future = func(lua, args);
let _sched = lua let sched = lua
.app_data_ref::<&Scheduler>() .app_data_ref::<&Scheduler>()
.expect("Lua struct is missing scheduler"); .expect("Lua struct is missing scheduler");
// FIXME: `self` escapes outside of method // FIXME: `self` escapes outside of method because we are borrowing `func`?
// sched.schedule_future_thread(thread, future)?; // sched.schedule_future_thread(thread, future)?;
Ok(()) Ok(())
}), }),