From b79d3ce4e2c6f90e4e33c0f86643bfcf85a8aec3 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Thu, 24 Aug 2023 19:09:05 -0500 Subject: [PATCH] Make clippy happy --- src/lune/builtins/luau/options.rs | 2 +- src/lune/builtins/net/websocket.rs | 23 +++++++++++++++-------- src/lune/scheduler/impl_runner.rs | 7 ++++++- src/lune/scheduler/impl_threads.rs | 30 +++++++++++++++++------------- src/lune/scheduler/mod.rs | 10 +++++----- src/lune/util/formatting.rs | 4 ++-- 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/lune/builtins/luau/options.rs b/src/lune/builtins/luau/options.rs index d1febe6..18bc8be 100644 --- a/src/lune/builtins/luau/options.rs +++ b/src/lune/builtins/luau/options.rs @@ -39,7 +39,7 @@ impl<'lua> FromLua<'lua> for LuauCompileOptions { let get_and_check = |name: &'static str| -> LuaResult> { match t.get(name)? { - Some(n @ (0 | 1 | 2)) => Ok(Some(n)), + Some(n @ (0..=2)) => Ok(Some(n)), Some(n) => Err(LuaError::runtime(format!( "'{name}' must be one of: 0, 1, or 2 - got {n}" ))), diff --git a/src/lune/builtins/net/websocket.rs b/src/lune/builtins/net/websocket.rs index 2bd7c5a..aa01c13 100644 --- a/src/lune/builtins/net/websocket.rs +++ b/src/lune/builtins/net/websocket.rs @@ -1,4 +1,4 @@ -use std::{cell::Cell, sync::Arc}; +use std::sync::Arc; use hyper::upgrade::Upgraded; use mlua::prelude::*; @@ -46,7 +46,7 @@ return freeze(setmetatable({ #[derive(Debug)] pub struct NetWebSocket { - close_code: Arc>>, + close_code: Arc>>, read_stream: Arc>>>, write_stream: Arc, WsMessage>>>, } @@ -69,7 +69,7 @@ where let (write, read) = value.split(); Self { - close_code: Arc::new(Cell::new(None)), + close_code: Arc::new(AsyncMutex::new(None)), read_stream: Arc::new(AsyncMutex::new(read)), write_stream: Arc::new(AsyncMutex::new(write)), } @@ -137,10 +137,16 @@ fn close_code<'lua, T>( where T: AsyncRead + AsyncWrite + Unpin, { - Ok(match socket.close_code.get() { - Some(code) => LuaValue::Number(code as f64), - None => LuaValue::Nil, - }) + Ok( + match *socket + .close_code + .try_lock() + .expect("Failed to lock close code") + { + Some(code) => LuaValue::Number(code as f64), + None => LuaValue::Nil, + }, + ) } async fn close<'lua, T>( @@ -204,7 +210,8 @@ where let msg = match item { Ok(Some(WsMessage::Close(msg))) => { if let Some(msg) = &msg { - socket.close_code.replace(Some(msg.code.into())); + let mut code = socket.close_code.lock().await; + *code = Some(msg.code.into()); } Ok(Some(WsMessage::Close(msg))) } diff --git a/src/lune/scheduler/impl_runner.rs b/src/lune/scheduler/impl_runner.rs index b4c1c61..55505a0 100644 --- a/src/lune/scheduler/impl_runner.rs +++ b/src/lune/scheduler/impl_runner.rs @@ -57,7 +57,12 @@ impl<'fut> Scheduler<'fut> { if thread.status() != LuaThreadStatus::Resumable { // NOTE: Threads that were spawned to resume // with an error will not have a result sender - if let Some(sender) = self.thread_senders.borrow_mut().remove(&thread_id) { + if let Some(sender) = self + .thread_senders + .try_lock() + .expect("Failed to get thread senders") + .remove(&thread_id) + { if sender.receiver_count() > 0 { let stored = match res { Err(e) => Err(e), diff --git a/src/lune/scheduler/impl_threads.rs b/src/lune/scheduler/impl_threads.rs index 850d3c2..8f3d174 100644 --- a/src/lune/scheduler/impl_threads.rs +++ b/src/lune/scheduler/impl_threads.rs @@ -14,8 +14,8 @@ impl<'fut> Scheduler<'fut> { pub(super) fn has_thread(&self) -> bool { !self .threads - .try_borrow() - .expect("Failed to borrow threads vec") + .try_lock() + .expect("Failed to lock threads vec") .is_empty() } @@ -27,9 +27,9 @@ impl<'fut> Scheduler<'fut> { pub(super) fn pop_thread(&self) -> LuaResult> { match self .threads - .try_borrow_mut() + .try_lock() .into_lua_err() - .context("Failed to borrow threads vec")? + .context("Failed to lock threads vec")? .pop_front() { Some(thread) => Ok(Some(thread)), @@ -54,9 +54,9 @@ impl<'fut> Scheduler<'fut> { self.state.set_thread_error(thread_id, err); self.threads - .try_borrow_mut() + .try_lock() .into_lua_err() - .context("Failed to borrow threads vec")? + .context("Failed to lock threads vec")? .push_front(thread); // NOTE: We might be resuming futures, need to signal that a @@ -83,16 +83,18 @@ impl<'fut> Scheduler<'fut> { let thread_id = thread.id(); self.threads - .try_borrow_mut() + .try_lock() .into_lua_err() - .context("Failed to borrow threads vec")? + .context("Failed to lock threads vec")? .push_front(thread); // NOTE: We might be resuming the same thread several times and // pushing it to the scheduler several times before it is done, // and we should only ever create one result sender per thread self.thread_senders - .borrow_mut() + .try_lock() + .into_lua_err() + .context("Failed to lock thread senders vec")? .entry(thread_id) .or_insert_with(|| SchedulerThreadSender::new(1)); @@ -120,16 +122,18 @@ impl<'fut> Scheduler<'fut> { let thread_id = thread.id(); self.threads - .try_borrow_mut() + .try_lock() .into_lua_err() - .context("Failed to borrow threads vec")? + .context("Failed to lock threads vec")? .push_back(thread); // NOTE: We might be resuming the same thread several times and // pushing it to the scheduler several times before it is done, // and we should only ever create one result sender per thread self.thread_senders - .borrow_mut() + .try_lock() + .into_lua_err() + .context("Failed to lock thread senders vec")? .entry(thread_id) .or_insert_with(|| SchedulerThreadSender::new(1)); @@ -149,7 +153,7 @@ impl<'fut> Scheduler<'fut> { thread_id: SchedulerThreadId, ) -> LuaResult> { let mut recv = { - let senders = self.thread_senders.borrow(); + let senders = self.thread_senders.lock().await; let sender = senders .get(&thread_id) .expect("Tried to wait for thread that is not queued"); diff --git a/src/lune/scheduler/mod.rs b/src/lune/scheduler/mod.rs index e153234..df1a087 100644 --- a/src/lune/scheduler/mod.rs +++ b/src/lune/scheduler/mod.rs @@ -1,5 +1,4 @@ use std::{ - cell::RefCell, collections::{HashMap, VecDeque}, pin::Pin, sync::Arc, @@ -37,8 +36,8 @@ type SchedulerFuture<'fut> = Pin + 'fut>>; #[derive(Debug, Clone)] pub(crate) struct Scheduler<'fut> { state: Arc, - threads: Arc>>, - thread_senders: Arc>>, + threads: Arc>>, + thread_senders: Arc>>, /* FUTURE: Get rid of these, let the tokio runtime handle running and resumption of futures completely, just use our scheduler @@ -64,11 +63,12 @@ impl<'fut> Scheduler<'fut> { /** Creates a new scheduler. */ + #[allow(clippy::arc_with_non_send_sync)] // FIXME: Clippy lints our tokio mutexes that are definitely Send + Sync pub fn new() -> Self { Self { state: Arc::new(SchedulerState::new()), - threads: Arc::new(RefCell::new(VecDeque::new())), - thread_senders: Arc::new(RefCell::new(HashMap::new())), + threads: Arc::new(AsyncMutex::new(VecDeque::new())), + thread_senders: Arc::new(AsyncMutex::new(HashMap::new())), futures_lua: Arc::new(AsyncMutex::new(FuturesUnordered::new())), futures_background: Arc::new(AsyncMutex::new(FuturesUnordered::new())), } diff --git a/src/lune/util/formatting.rs b/src/lune/util/formatting.rs index 165ceb4..2107c14 100644 --- a/src/lune/util/formatting.rs +++ b/src/lune/util/formatting.rs @@ -118,8 +118,8 @@ pub fn pretty_format_value( COLOR_GREEN.apply_to( s.to_string_lossy() .replace('"', r#"\""#) - .replace('\r', r#"\r"#) - .replace('\n', r#"\n"#) + .replace('\r', r"\r") + .replace('\n', r"\n") ) )?, LuaValue::Table(ref tab) => {