mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Use static lifetime bound instead of spamming actual static lifetime
This commit is contained in:
parent
0dbf466817
commit
7fe43a969f
2 changed files with 28 additions and 24 deletions
|
@ -13,29 +13,32 @@ return yield()
|
|||
for access to the scheduler without having to import
|
||||
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
|
||||
closure and returns the results of it to the call site.
|
||||
*/
|
||||
fn create_async_function<A, R, F, FR>(
|
||||
&'static self,
|
||||
func: F,
|
||||
) -> LuaResult<LuaFunction<'static>>
|
||||
fn create_async_function<A, R, F, FR>(&'lua self, func: F) -> LuaResult<LuaFunction<'lua>>
|
||||
where
|
||||
A: FromLuaMulti<'static>,
|
||||
R: IntoLuaMulti<'static>,
|
||||
F: Fn(&'static Lua, A) -> FR + 'static,
|
||||
FR: Future<Output = LuaResult<R>> + 'static;
|
||||
A: FromLuaMulti<'lua>,
|
||||
R: IntoLuaMulti<'lua>,
|
||||
F: Fn(&'lua Lua, A) -> FR + 'lua,
|
||||
FR: Future<Output = LuaResult<R>> + 'lua;
|
||||
}
|
||||
|
||||
impl LuaSchedulerExt for Lua {
|
||||
fn create_async_function<A, R, F, FR>(&'static self, func: F) -> LuaResult<LuaFunction<'static>>
|
||||
// FIXME: `self` escapes outside of method because we are borrowing `func`
|
||||
// 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
|
||||
A: FromLuaMulti<'static>,
|
||||
R: IntoLuaMulti<'static>,
|
||||
F: Fn(&'static Lua, A) -> FR + 'static,
|
||||
FR: Future<Output = LuaResult<R>> + 'static,
|
||||
'lua: 'static,
|
||||
{
|
||||
fn create_async_function<A, R, F, FR>(&'lua self, func: F) -> LuaResult<LuaFunction<'lua>>
|
||||
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)?;
|
||||
|
||||
|
@ -54,8 +57,6 @@ impl LuaSchedulerExt for Lua {
|
|||
let sched = lua
|
||||
.app_data_ref::<&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)?;
|
||||
Ok(())
|
||||
}),
|
||||
|
|
|
@ -52,15 +52,18 @@ impl<'lua> TableBuilder<'lua> {
|
|||
}
|
||||
}
|
||||
|
||||
// FIXME: Remove static lifetimes here when possible and move into above impl
|
||||
impl TableBuilder<'static> {
|
||||
// FIXME: Remove static lifetime bound here when possible and move into above impl
|
||||
impl<'lua> TableBuilder<'lua>
|
||||
where
|
||||
'lua: 'static,
|
||||
{
|
||||
pub fn with_async_function<K, A, R, F, FR>(self, key: K, func: F) -> LuaResult<Self>
|
||||
where
|
||||
K: IntoLua<'static>,
|
||||
A: FromLuaMulti<'static>,
|
||||
R: IntoLuaMulti<'static>,
|
||||
F: Fn(&'static Lua, A) -> FR + 'static,
|
||||
FR: Future<Output = LuaResult<R>> + 'static,
|
||||
K: IntoLua<'lua>,
|
||||
A: FromLuaMulti<'lua>,
|
||||
R: IntoLuaMulti<'lua>,
|
||||
F: Fn(&'lua Lua, A) -> FR + 'lua,
|
||||
FR: Future<Output = LuaResult<R>> + 'lua,
|
||||
{
|
||||
let f = self.lua.create_async_function(func)?;
|
||||
self.with_value(key, LuaValue::Function(f))
|
||||
|
|
Loading…
Reference in a new issue