diff --git a/src/lune/scheduler/traits.rs b/src/lune/scheduler/traits.rs index fad4f24..3531c7b 100644 --- a/src/lune/scheduler/traits.rs +++ b/src/lune/scheduler/traits.rs @@ -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( - &'static self, - func: F, - ) -> LuaResult> + fn create_async_function(&'lua self, func: F) -> LuaResult> where - A: FromLuaMulti<'static>, - R: IntoLuaMulti<'static>, - F: Fn(&'static Lua, A) -> FR + 'static, - FR: Future> + 'static; + A: FromLuaMulti<'lua>, + R: IntoLuaMulti<'lua>, + F: Fn(&'lua Lua, A) -> FR + 'lua, + FR: Future> + 'lua; } -impl LuaSchedulerExt for Lua { - fn create_async_function(&'static self, func: F) -> LuaResult> +// 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 + 'lua: 'static, +{ + fn create_async_function(&'lua self, func: F) -> LuaResult> where - A: FromLuaMulti<'static>, - R: IntoLuaMulti<'static>, - F: Fn(&'static Lua, A) -> FR + 'static, - FR: Future> + 'static, + A: FromLuaMulti<'lua>, + R: IntoLuaMulti<'lua>, + F: Fn(&'lua Lua, A) -> FR + 'lua, + FR: Future> + '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(()) }), diff --git a/src/lune/util/table_builder.rs b/src/lune/util/table_builder.rs index 9cc6a4e..7db4910 100644 --- a/src/lune/util/table_builder.rs +++ b/src/lune/util/table_builder.rs @@ -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(self, key: K, func: F) -> LuaResult where - K: IntoLua<'static>, - A: FromLuaMulti<'static>, - R: IntoLuaMulti<'static>, - F: Fn(&'static Lua, A) -> FR + 'static, - FR: Future> + 'static, + K: IntoLua<'lua>, + A: FromLuaMulti<'lua>, + R: IntoLuaMulti<'lua>, + F: Fn(&'lua Lua, A) -> FR + 'lua, + FR: Future> + 'lua, { let f = self.lua.create_async_function(func)?; self.with_value(key, LuaValue::Function(f))