From 88494fedcbfcb9d1a48e8fe657f8f60b38ddbb47 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Thu, 24 Apr 2025 21:52:07 +0200 Subject: [PATCH] Migrate lune-std to use async-fs and friends --- Cargo.lock | 4 ++- crates/lune-std/Cargo.toml | 5 +++- .../lune-std/src/globals/require/context.rs | 27 ++++++++----------- crates/lune-std/src/luaurc.rs | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 80b453c..623019e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1735,6 +1735,9 @@ dependencies = [ name = "lune-std" version = "0.1.5" dependencies = [ + "async-channel", + "async-fs", + "async-lock", "lune-std-datetime", "lune-std-fs", "lune-std-luau", @@ -1750,7 +1753,6 @@ dependencies = [ "mlua-luau-scheduler", "serde", "serde_json", - "tokio", ] [[package]] diff --git a/crates/lune-std/Cargo.toml b/crates/lune-std/Cargo.toml index fd1e803..3900e0e 100644 --- a/crates/lune-std/Cargo.toml +++ b/crates/lune-std/Cargo.toml @@ -41,9 +41,12 @@ task = ["dep:lune-std-task"] mlua = { version = "0.10.3", features = ["luau"] } mlua-luau-scheduler = { version = "0.0.2", path = "../mlua-luau-scheduler" } +async-channel = "2.3" +async-fs = "2.1" +async-lock = "3.4" + serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -tokio = { version = "1", default-features = false, features = ["fs", "sync"] } lune-utils = { version = "0.1.3", path = "../lune-utils" } diff --git a/crates/lune-std/src/globals/require/context.rs b/crates/lune-std/src/globals/require/context.rs index 476583d..6787867 100644 --- a/crates/lune-std/src/globals/require/context.rs +++ b/crates/lune-std/src/globals/require/context.rs @@ -7,13 +7,9 @@ use std::{ use mlua::prelude::*; use mlua_luau_scheduler::LuaSchedulerExt; -use tokio::{ - fs::read, - sync::{ - broadcast::{self, Sender}, - Mutex as AsyncMutex, - }, -}; +use async_channel::{unbounded, Receiver}; +use async_fs::read; +use async_lock::Mutex as AsyncMutex; use lune_utils::path::{clean_path, clean_path_and_make_absolute}; @@ -29,7 +25,7 @@ use crate::library::LuneStandardLibrary; pub(super) struct RequireContext { libraries: Arc>>>, results: Arc>>>, - pending: Arc>>>, + pending: Arc>>>, } impl RequireContext { @@ -160,15 +156,15 @@ impl RequireContext { lua: Lua, abs_path: impl AsRef, ) -> LuaResult { - let mut thread_recv = { + let thread_recv = { let pending = self .pending .try_lock() .expect("RequireContext may not be used from multiple threads"); - let thread_id = pending + let thread_recv = pending .get(abs_path.as_ref()) .expect("Path is not currently pending require"); - thread_id.subscribe() + thread_recv.clone() }; thread_recv.recv().await.into_lua_err()?; @@ -224,11 +220,11 @@ impl RequireContext { let rel_path = rel_path.as_ref(); // Set this abs path as currently pending - let (broadcast_tx, _) = broadcast::channel(1); + let (broadcast_tx, broadcast_rx) = unbounded(); self.pending .try_lock() .expect("RequireContext may not be used from multiple threads") - .insert(abs_path.to_path_buf(), broadcast_tx); + .insert(abs_path.to_path_buf(), broadcast_rx); // Try to load at this abs path let load_res = self.load(lua.clone(), abs_path, rel_path).await; @@ -253,13 +249,12 @@ impl RequireContext { // Remove the pending thread id from the require context, // broadcast a message to let any listeners know that this // path has now finished the require process and is cached - let broadcast_tx = self - .pending + self.pending .try_lock() .expect("RequireContext may not be used from multiple threads") .remove(abs_path) .expect("Pending require broadcaster was unexpectedly removed"); - broadcast_tx.send(()).ok(); + broadcast_tx.send(()).await.ok(); load_val } diff --git a/crates/lune-std/src/luaurc.rs b/crates/lune-std/src/luaurc.rs index 0eada59..1d050fd 100644 --- a/crates/lune-std/src/luaurc.rs +++ b/crates/lune-std/src/luaurc.rs @@ -4,9 +4,9 @@ use std::{ sync::Arc, }; +use async_fs::read; use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; -use tokio::fs::read; use lune_utils::path::{clean_path, clean_path_and_make_absolute};