From 34529c02352e616cef6d12de49eeb31f6a2742fb Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Fri, 19 Jan 2024 11:22:51 +0100 Subject: [PATCH] Clean up lib and only export necessities --- lib/{thread_callbacks.rs => callbacks.rs} | 6 ++-- lib/lib.rs | 13 ++++++--- lib/{thread_runtime.rs => runtime.rs} | 20 ++++++------- lib/{thread_storage.rs => storage.rs} | 2 +- lib/traits.rs | 34 ++++++++++++++++++++++ lib/{thread_util.rs => util.rs} | 35 +---------------------- src/main.rs | 9 ++---- 7 files changed, 60 insertions(+), 59 deletions(-) rename lib/{thread_callbacks.rs => callbacks.rs} (96%) rename lib/{thread_runtime.rs => runtime.rs} (92%) rename lib/{thread_storage.rs => storage.rs} (97%) create mode 100644 lib/traits.rs rename lib/{thread_util.rs => util.rs} (52%) diff --git a/lib/thread_callbacks.rs b/lib/callbacks.rs similarity index 96% rename from lib/thread_callbacks.rs rename to lib/callbacks.rs index 55587d3..180c25c 100644 --- a/lib/thread_callbacks.rs +++ b/lib/callbacks.rs @@ -4,13 +4,13 @@ type ErrorCallback = Box Fn(&'lua Lua, LuaThread<'lua>, LuaError) type ValueCallback = Box Fn(&'lua Lua, LuaThread<'lua>, LuaValue<'lua>) + 'static>; #[derive(Default)] -pub struct ThreadCallbacks { +pub struct Callbacks { on_error: Option, on_value: Option, } -impl ThreadCallbacks { - pub fn new() -> ThreadCallbacks { +impl Callbacks { + pub fn new() -> Callbacks { Default::default() } diff --git a/lib/lib.rs b/lib/lib.rs index a644945..c5a7205 100644 --- a/lib/lib.rs +++ b/lib/lib.rs @@ -1,7 +1,12 @@ -pub mod thread_callbacks; -pub mod thread_runtime; -pub mod thread_storage; -pub mod thread_util; +mod callbacks; +mod runtime; +mod storage; +mod traits; +mod util; pub use mlua; pub use smol; + +pub use callbacks::Callbacks; +pub use runtime::Runtime; +pub use traits::IntoLuaThread; diff --git a/lib/thread_runtime.rs b/lib/runtime.rs similarity index 92% rename from lib/thread_runtime.rs rename to lib/runtime.rs index 8b4de35..ff46706 100644 --- a/lib/thread_runtime.rs +++ b/lib/runtime.rs @@ -10,12 +10,10 @@ use smol::{ }; use super::{ - thread_callbacks::ThreadCallbacks, - thread_storage::ThreadWithArgs, - thread_util::{IntoLuaThread, LuaThreadOrFunction}, + callbacks::Callbacks, storage::ThreadWithArgs, traits::IntoLuaThread, util::LuaThreadOrFunction, }; -pub struct ThreadRuntime { +pub struct Runtime { queue_status: Rc>, queue_spawn: Rc>>, queue_defer: Rc>>, @@ -23,13 +21,13 @@ pub struct ThreadRuntime { rx: Receiver<()>, } -impl ThreadRuntime { +impl Runtime { /** Creates a new runtime for the given Lua state. This will inject some functions to interact with the scheduler / executor. */ - pub fn new(lua: &Lua) -> LuaResult { + pub fn new(lua: &Lua) -> LuaResult { let queue_status = Rc::new(Cell::new(false)); let queue_spawn = Rc::new(Mutex::new(Vec::new())); let queue_defer = Rc::new(Mutex::new(Vec::new())); @@ -65,8 +63,8 @@ impl ThreadRuntime { LuaError::runtime("Tried to spawn thread to a dropped queue") })?; } - Ok(v) => ThreadCallbacks::forward_value(lua, thread.clone(), v), - Err(e) => ThreadCallbacks::forward_error(lua, thread.clone(), e), + Ok(v) => Callbacks::forward_value(lua, thread.clone(), v), + Err(e) => Callbacks::forward_error(lua, thread.clone(), e), } Ok(thread) } else { @@ -101,7 +99,7 @@ impl ThreadRuntime { lua.globals().set("spawn", fn_spawn)?; lua.globals().set("defer", fn_defer)?; - Ok(ThreadRuntime { + Ok(Runtime { queue_status, queue_spawn, queue_defer, @@ -182,8 +180,8 @@ impl ThreadRuntime { // drop it right away to clear stack space since detached tasks dont drop // until the executor drops https://github.com/smol-rs/smol/issues/294 match stream.next().await.unwrap() { - Ok(v) => ThreadCallbacks::forward_value(lua, thread, v), - Err(e) => ThreadCallbacks::forward_error(lua, thread, e), + Ok(v) => Callbacks::forward_value(lua, thread, v), + Err(e) => Callbacks::forward_error(lua, thread, e), }; }) .detach(); diff --git a/lib/thread_storage.rs b/lib/storage.rs similarity index 97% rename from lib/thread_storage.rs rename to lib/storage.rs index cb51c6a..f067983 100644 --- a/lib/thread_storage.rs +++ b/lib/storage.rs @@ -1,7 +1,7 @@ use mlua::prelude::*; #[derive(Debug)] -pub struct ThreadWithArgs { +pub(crate) struct ThreadWithArgs { key_thread: LuaRegistryKey, key_args: LuaRegistryKey, } diff --git a/lib/traits.rs b/lib/traits.rs new file mode 100644 index 0000000..ec9c04f --- /dev/null +++ b/lib/traits.rs @@ -0,0 +1,34 @@ +use mlua::prelude::*; + +/** + Trait for any struct that can be turned into an [`LuaThread`] + and passed to the runtime, implemented for the following types: + + - Lua threads ([`LuaThread`]) + - Lua functions ([`LuaFunction`]) + - Lua chunks ([`LuaChunk`]) +*/ +pub trait IntoLuaThread<'lua> { + /** + Converts the value into a Lua thread. + */ + fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult>; +} + +impl<'lua> IntoLuaThread<'lua> for LuaThread<'lua> { + fn into_lua_thread(self, _: &'lua Lua) -> LuaResult> { + Ok(self) + } +} + +impl<'lua> IntoLuaThread<'lua> for LuaFunction<'lua> { + fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult> { + lua.create_thread(self) + } +} + +impl<'lua> IntoLuaThread<'lua> for LuaChunk<'lua, '_> { + fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult> { + lua.create_thread(self.into_function()?) + } +} diff --git a/lib/thread_util.rs b/lib/util.rs similarity index 52% rename from lib/thread_util.rs rename to lib/util.rs index 43e0858..089c223 100644 --- a/lib/thread_util.rs +++ b/lib/util.rs @@ -6,7 +6,7 @@ use mlua::prelude::*; [`LuaThreadOrFunction::into_thread`] may be used to convert the value into a Lua thread. */ #[derive(Clone)] -pub enum LuaThreadOrFunction<'lua> { +pub(crate) enum LuaThreadOrFunction<'lua> { Thread(LuaThread<'lua>), Function(LuaFunction<'lua>), } @@ -33,36 +33,3 @@ impl<'lua> FromLua<'lua> for LuaThreadOrFunction<'lua> { } } } - -/** - Trait for any struct that can be turned into an [`LuaThread`] - and given to the scheduler, implemented for the following types: - - - Lua threads ([`LuaThread`]) - - Lua functions ([`LuaFunction`]) - - Lua chunks ([`LuaChunk`]) -*/ -pub trait IntoLuaThread<'lua> { - /** - Converts the value into a Lua thread. - */ - fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult>; -} - -impl<'lua> IntoLuaThread<'lua> for LuaThread<'lua> { - fn into_lua_thread(self, _: &'lua Lua) -> LuaResult> { - Ok(self) - } -} - -impl<'lua> IntoLuaThread<'lua> for LuaFunction<'lua> { - fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult> { - lua.create_thread(self) - } -} - -impl<'lua, 'a> IntoLuaThread<'lua> for LuaChunk<'lua, 'a> { - fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult> { - lua.create_thread(self.into_function()?) - } -} diff --git a/src/main.rs b/src/main.rs index 499b588..88fd917 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,9 @@ use std::time::{Duration, Instant}; -use mlua::prelude::*; -use smol::*; +use smol_mlua::{mlua::prelude::*, smol::*, Callbacks, Runtime}; const MAIN_SCRIPT: &str = include_str!("./main.luau"); -use smol_mlua::{thread_callbacks::ThreadCallbacks, thread_runtime::ThreadRuntime}; - pub fn main() -> LuaResult<()> { let start = Instant::now(); let lua = Lua::new(); @@ -23,12 +20,12 @@ pub fn main() -> LuaResult<()> { )?; // Set up runtime (thread queue / async executors) - let rt = ThreadRuntime::new(&lua)?; + let rt = Runtime::new(&lua)?; let main = rt.push_main(&lua, lua.load(MAIN_SCRIPT), ()); lua.set_named_registry_value("main", main)?; // Add callbacks to capture resulting value/error of main thread - ThreadCallbacks::new() + Callbacks::new() .on_value(|lua, thread, val| { let main = lua.named_registry_value::("main").unwrap(); if main == thread {