diff --git a/src/lune/mod.rs b/src/lune/mod.rs index b46e023..dd79aac 100644 --- a/src/lune/mod.rs +++ b/src/lune/mod.rs @@ -39,8 +39,15 @@ impl Lune { script_name: impl AsRef, script_contents: impl AsRef<[u8]>, ) -> Result { - 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) } } diff --git a/src/lune/scheduler/impl_async.rs b/src/lune/scheduler/impl_async.rs index 65c1970..2754f40 100644 --- a/src/lune/scheduler/impl_async.rs +++ b/src/lune/scheduler/impl_async.rs @@ -24,12 +24,20 @@ where /** Schedules the given `thread` to run when the given `fut` completes. */ - pub fn schedule_future_thread(&'fut self, thread: LuaOwnedThread, fut: F) -> LuaResult<()> + pub fn schedule_future_thread( + &'fut self, + thread: LuaOwnedThread, + fut: F, + ) -> LuaResult<()> where - F: 'fut + Future>>, + FR: IntoLuaMulti<'fut>, + F: 'fut + Future>, { 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"); }); diff --git a/src/lune/scheduler/impl_runner.rs b/src/lune/scheduler/impl_runner.rs index ed84b92..2554c4a 100644 --- a/src/lune/scheduler/impl_runner.rs +++ b/src/lune/scheduler/impl_runner.rs @@ -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, - 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 - } } diff --git a/src/lune/scheduler/impl_threads.rs b/src/lune/scheduler/impl_threads.rs index b7dea8a..638416d 100644 --- a/src/lune/scheduler/impl_threads.rs +++ b/src/lune/scheduler/impl_threads.rs @@ -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 { + 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 { + 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(); diff --git a/src/lune/scheduler/traits.rs b/src/lune/scheduler/traits.rs index 3bbd129..9457843 100644 --- a/src/lune/scheduler/traits.rs +++ b/src/lune/scheduler/traits.rs @@ -26,7 +26,7 @@ where A: FromLuaMulti<'lua>, R: IntoLuaMulti<'lua>, F: 'static + Fn(&'lua Lua, A) -> FR, - FR: 'fut + Future>; + FR: 'static + Future>; } 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>, + FR: 'static + Future>, { let async_env = self.create_table_with_capacity(0, 2)?;