Use static lifetime bound instead of spamming actual static lifetime

This commit is contained in:
Filip Tibell 2023-08-18 14:03:06 -05:00
parent 0dbf466817
commit 7fe43a969f
2 changed files with 28 additions and 24 deletions

View file

@ -13,29 +13,32 @@ return yield()
for access to the scheduler without having to import for access to the scheduler without having to import
it or handle registry / app data references manually. it or handle registry / app data references manually.
*/ */
pub trait LuaSchedulerExt { pub trait LuaSchedulerExt<'lua> {
/** /**
Creates a function callable from Lua that runs an async Creates a function callable from Lua that runs an async
closure and returns the results of it to the call site. closure and returns the results of it to the call site.
*/ */
fn create_async_function<A, R, F, FR>( fn create_async_function<A, R, F, FR>(&'lua self, func: F) -> LuaResult<LuaFunction<'lua>>
&'static self,
func: F,
) -> LuaResult<LuaFunction<'static>>
where where
A: FromLuaMulti<'static>, A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'static>, R: IntoLuaMulti<'lua>,
F: Fn(&'static Lua, A) -> FR + 'static, F: Fn(&'lua Lua, A) -> FR + 'lua,
FR: Future<Output = LuaResult<R>> + 'static; FR: Future<Output = LuaResult<R>> + 'lua;
} }
impl LuaSchedulerExt for Lua { // FIXME: `self` escapes outside of method because we are borrowing `func`
fn create_async_function<A, R, F, FR>(&'static self, func: F) -> LuaResult<LuaFunction<'static>> // when we call `schedule_future_thread` in the lua function body below
// For now we solve this by using the 'static lifetime bound in the impl
impl<'lua> LuaSchedulerExt<'lua> for Lua
where where
A: FromLuaMulti<'static>, 'lua: 'static,
R: IntoLuaMulti<'static>, {
F: Fn(&'static Lua, A) -> FR + 'static, fn create_async_function<A, R, F, FR>(&'lua self, func: F) -> LuaResult<LuaFunction<'lua>>
FR: Future<Output = LuaResult<R>> + 'static, where
A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'lua>,
F: Fn(&'lua Lua, A) -> FR + 'lua,
FR: Future<Output = LuaResult<R>> + 'lua,
{ {
let async_env = self.create_table_with_capacity(0, 2)?; let async_env = self.create_table_with_capacity(0, 2)?;
@ -54,8 +57,6 @@ impl LuaSchedulerExt for Lua {
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 because we are borrowing `func`?
// For now we solve this by using 'static lifetimes for this entire method
sched.schedule_future_thread(thread, future)?; sched.schedule_future_thread(thread, future)?;
Ok(()) Ok(())
}), }),

View file

@ -52,15 +52,18 @@ impl<'lua> TableBuilder<'lua> {
} }
} }
// FIXME: Remove static lifetimes here when possible and move into above impl // FIXME: Remove static lifetime bound here when possible and move into above impl
impl TableBuilder<'static> { impl<'lua> TableBuilder<'lua>
where
'lua: 'static,
{
pub fn with_async_function<K, A, R, F, FR>(self, key: K, func: F) -> LuaResult<Self> pub fn with_async_function<K, A, R, F, FR>(self, key: K, func: F) -> LuaResult<Self>
where where
K: IntoLua<'static>, K: IntoLua<'lua>,
A: FromLuaMulti<'static>, A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'static>, R: IntoLuaMulti<'lua>,
F: Fn(&'static Lua, A) -> FR + 'static, F: Fn(&'lua Lua, A) -> FR + 'lua,
FR: Future<Output = LuaResult<R>> + 'static, FR: Future<Output = LuaResult<R>> + 'lua,
{ {
let f = self.lua.create_async_function(func)?; let f = self.lua.create_async_function(func)?;
self.with_value(key, LuaValue::Function(f)) self.with_value(key, LuaValue::Function(f))