Relax scheduler method args since lifetimes are now correct

This commit is contained in:
Filip Tibell 2023-08-18 09:36:08 -05:00
parent dc80b1c28f
commit 62e6130223
5 changed files with 38 additions and 46 deletions

View file

@ -39,8 +39,15 @@ impl Lune {
script_name: impl AsRef<str>,
script_contents: impl AsRef<[u8]>,
) -> Result<ExitCode, LuneError> {
Ok(Scheduler::new()
.run_main(script_name, script_contents)
.await)
let scheduler = Scheduler::new();
let main = scheduler
.lua
.load(script_contents.as_ref())
.set_name(script_name.as_ref());
scheduler.push_back(main, ())?;
Ok(scheduler.run_to_completion().await)
}
}

View file

@ -24,12 +24,20 @@ where
/**
Schedules the given `thread` to run when the given `fut` completes.
*/
pub fn schedule_future_thread<F>(&'fut self, thread: LuaOwnedThread, fut: F) -> LuaResult<()>
pub fn schedule_future_thread<F, FR>(
&'fut self,
thread: LuaOwnedThread,
fut: F,
) -> LuaResult<()>
where
F: 'fut + Future<Output = LuaResult<LuaMultiValue<'fut>>>,
FR: IntoLuaMulti<'fut>,
F: 'fut + Future<Output = LuaResult<FR>>,
{
self.schedule_future(async move {
let rets = fut.await.expect("Failed to receive result");
let rets = rets
.into_lua_multi(&self.lua)
.expect("Failed to create return multi value");
self.push_back(thread, rets)
.expect("Failed to schedule future thread");
});

View file

@ -5,9 +5,7 @@ use mlua::prelude::*;
use tokio::task::LocalSet;
use super::{IntoLuaOwnedThread, Scheduler};
const EMPTY_MULTI_VALUE: LuaMultiValue = LuaMultiValue::new();
use super::Scheduler;
impl<'lua, 'fut> Scheduler<'fut>
where
@ -121,31 +119,4 @@ where
ExitCode::SUCCESS
}
}
/**
Runs a script with the given `script_name` and `script_contents` to completion.
Refer to [`run_to_completion`] for additional details.
*/
pub async fn run_main(
self,
script_name: impl AsRef<str>,
script_contents: impl AsRef<[u8]>,
) -> ExitCode {
let main_fn = self
.lua
.load(script_contents.as_ref())
.set_name(script_name.as_ref())
.into_function()
.expect("Failed to create function for main");
let main_thread = main_fn
.into_owned_lua_thread(&self.lua)
.expect("Failed to create thread for main");
self.push_back(main_thread, EMPTY_MULTI_VALUE)
.expect("Failed to enqueue thread for main");
self.run_to_completion().await
}
}

View file

@ -4,7 +4,7 @@ use mlua::prelude::*;
use super::{
thread::{SchedulerThread, SchedulerThreadId, SchedulerThreadSender},
Scheduler,
IntoLuaOwnedThread, Scheduler,
};
impl<'lua, 'fut> Scheduler<'fut>
@ -55,11 +55,14 @@ where
Schedules the `thread` to be resumed with the given `args`
right away, before any other currently scheduled threads.
*/
pub fn push_front(
&self,
thread: LuaOwnedThread,
args: LuaMultiValue<'_>,
pub fn push_front<'a>(
&'a self,
thread: impl IntoLuaOwnedThread,
args: impl IntoLuaMulti<'a>,
) -> LuaResult<SchedulerThreadId> {
let thread = thread.into_owned_lua_thread(&self.lua)?;
let args = args.into_lua_multi(&self.lua)?;
let thread = SchedulerThread::new(&self.lua, thread, args)?;
let thread_id = thread.id();
@ -79,11 +82,14 @@ where
Schedules the `thread` to be resumed with the given `args`
after all other current threads have been resumed.
*/
pub fn push_back(
&self,
thread: LuaOwnedThread,
args: LuaMultiValue<'_>,
pub fn push_back<'a>(
&'a self,
thread: impl IntoLuaOwnedThread,
args: impl IntoLuaMulti<'a>,
) -> LuaResult<SchedulerThreadId> {
let thread = thread.into_owned_lua_thread(&self.lua)?;
let args = args.into_lua_multi(&self.lua)?;
let thread = SchedulerThread::new(&self.lua, thread, args)?;
let thread_id = thread.id();

View file

@ -26,7 +26,7 @@ where
A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'lua>,
F: 'static + Fn(&'lua Lua, A) -> FR,
FR: 'fut + Future<Output = LuaResult<R>>;
FR: 'static + Future<Output = LuaResult<R>>;
}
impl<'lua, 'fut> LuaSchedulerExt<'lua, 'fut> for Lua
@ -38,7 +38,7 @@ where
A: FromLuaMulti<'lua>,
R: IntoLuaMulti<'lua>,
F: 'static + Fn(&'lua Lua, A) -> FR,
FR: 'fut + Future<Output = LuaResult<R>>,
FR: 'static + Future<Output = LuaResult<R>>,
{
let async_env = self.create_table_with_capacity(0, 2)?;