From a448c9d2c64eea339ce672955d24c0b6ee170b8e Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Fri, 3 Feb 2023 14:15:20 -0500 Subject: [PATCH] Migrate back to tokio to prepare for using hyper --- Cargo.lock | 381 ++++++++++++++----------------------- Cargo.toml | 2 +- src/cli/cli.rs | 38 ++-- src/cli/main.rs | 10 +- src/cli/utils/github.rs | 5 +- src/cli/utils/listing.rs | 4 +- src/lib/globals/fs.rs | 4 +- src/lib/globals/process.rs | 4 +- src/lib/globals/task.rs | 4 +- src/lib/lib.rs | 69 ++++--- src/lib/utils/task.rs | 26 ++- 11 files changed, 214 insertions(+), 333 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e61e7dc..7fe648c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,115 +23,6 @@ version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" -[[package]] -name = "async-channel" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf46fee83e5ccffc220104713af3292ff9bc7c64c7de289f66dae8e38d826833" -dependencies = [ - "concurrent-queue", - "event-listener", - "futures-core", -] - -[[package]] -name = "async-executor" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17adb73da160dfb475c183343c8cccd80721ea5a605d3eb57125f0a7b7a92d0b" -dependencies = [ - "async-lock", - "async-task", - "concurrent-queue", - "fastrand", - "futures-lite", - "slab", -] - -[[package]] -name = "async-fs" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "279cf904654eeebfa37ac9bb1598880884924aab82e290aa65c9e77a0e142e06" -dependencies = [ - "async-lock", - "autocfg", - "blocking", - "futures-lite", -] - -[[package]] -name = "async-io" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c374dda1ed3e7d8f0d9ba58715f924862c63eae6849c92d3a18e7fbde9e2794" -dependencies = [ - "async-lock", - "autocfg", - "concurrent-queue", - "futures-lite", - "libc", - "log", - "parking", - "polling", - "slab", - "socket2", - "waker-fn", - "windows-sys", -] - -[[package]] -name = "async-lock" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8101efe8695a6c17e02911402145357e718ac92d3ff88ae8419e84b1707b685" -dependencies = [ - "event-listener", - "futures-lite", -] - -[[package]] -name = "async-net" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4051e67316bc7eff608fe723df5d32ed639946adcd69e07df41fd42a7b411f1f" -dependencies = [ - "async-io", - "autocfg", - "blocking", - "futures-lite", -] - -[[package]] -name = "async-process" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6381ead98388605d0d9ff86371043b5aa922a3905824244de40dc263a14fcba4" -dependencies = [ - "async-io", - "async-lock", - "autocfg", - "blocking", - "cfg-if", - "event-listener", - "futures-lite", - "libc", - "signal-hook", - "windows-sys", -] - -[[package]] -name = "async-task" -version = "4.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a40729d2133846d9ed0ea60a8b9541bccddab49cd30f0715a1da672fe9a2524" - -[[package]] -name = "atomic-waker" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "debc29dde2e69f9e47506b525f639ed42300fc014a3e007832592448fa8e4599" - [[package]] name = "autocfg" version = "1.1.0" @@ -156,20 +47,6 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "blocking" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c67b173a56acffd6d2326fb7ab938ba0b00a71480e14902b2591c87bc5741e8" -dependencies = [ - "async-channel", - "async-lock", - "async-task", - "atomic-waker", - "fastrand", - "futures-lite", -] - [[package]] name = "bstr" version = "0.2.17" @@ -191,6 +68,12 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be0fdd54b507df8f22012890aadd099979befdba27713c767993f8380112ca7c" +[[package]] +name = "bytes" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" + [[package]] name = "cc" version = "1.0.78" @@ -240,15 +123,6 @@ dependencies = [ "os_str_bytes", ] -[[package]] -name = "concurrent-queue" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c278839b831783b70278b14df4d45e1beb1aad306c07bb796637de9a0e323e8e" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "convert_case" version = "0.4.0" @@ -264,15 +138,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "crossbeam-utils" -version = "0.8.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" -dependencies = [ - "cfg-if", -] - [[package]] name = "derive_more" version = "0.99.17" @@ -316,21 +181,6 @@ dependencies = [ "libc", ] -[[package]] -name = "event-listener" -version = "2.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" - -[[package]] -name = "fastrand" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" -dependencies = [ - "instant", -] - [[package]] name = "flate2" version = "1.0.25" @@ -390,27 +240,6 @@ version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac" -[[package]] -name = "futures-io" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00f5fb52a06bdcadeb54e8d3671f8888a39697dcb0b81b23b55174030427f4eb" - -[[package]] -name = "futures-lite" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48" -dependencies = [ - "fastrand", - "futures-core", - "futures-io", - "memchr", - "parking", - "pin-project-lite", - "waker-fn", -] - [[package]] name = "futures-macro" version = "0.3.25" @@ -483,15 +312,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "instant" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" -dependencies = [ - "cfg-if", -] - [[package]] name = "io-lifetimes" version = "1.0.4" @@ -499,7 +319,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7d6c6f8c91b4b9ed43484ad1a938e393caf35960fce7f82a040497207bd8e9e" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.42.0", ] [[package]] @@ -511,7 +331,7 @@ dependencies = [ "hermit-abi", "io-lifetimes", "rustix", - "windows-sys", + "windows-sys 0.42.0", ] [[package]] @@ -541,6 +361,16 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.17" @@ -594,7 +424,7 @@ dependencies = [ "regex", "serde", "serde_json", - "smol", + "tokio", "ureq", ] @@ -613,6 +443,18 @@ dependencies = [ "adler", ] +[[package]] +name = "mio" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys 0.42.0", +] + [[package]] name = "mlua" version = "0.8.7" @@ -642,6 +484,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.17.0" @@ -658,10 +510,27 @@ dependencies = [ ] [[package]] -name = "parking" -version = "2.0.0" +name = "parking_lot" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys 0.45.0", +] [[package]] name = "paste" @@ -706,20 +575,6 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" -[[package]] -name = "polling" -version = "2.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22122d5ec4f9fe1b3916419b76be1e80bcb93f618d071d2edf841b137b2a2bd6" -dependencies = [ - "autocfg", - "cfg-if", - "libc", - "log", - "wepoll-ffi", - "windows-sys", -] - [[package]] name = "proc-macro-error" version = "1.0.4" @@ -768,6 +623,15 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.7.1" @@ -826,7 +690,7 @@ dependencies = [ "io-lifetimes", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.42.0", ] [[package]] @@ -847,6 +711,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "sct" version = "0.7.0" @@ -894,16 +764,6 @@ dependencies = [ "serde", ] -[[package]] -name = "signal-hook" -version = "0.3.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a253b5e89e2698464fc26b545c9edceb338e18a89effeeecfea192c3025be29d" -dependencies = [ - "libc", - "signal-hook-registry", -] - [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -923,21 +783,10 @@ dependencies = [ ] [[package]] -name = "smol" -version = "1.3.0" +name = "smallvec" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13f2b548cd8447f8de0fdf1c592929f70f4fc7039a05e47404b0d096ec6987a1" -dependencies = [ - "async-channel", - "async-executor", - "async-fs", - "async-io", - "async-lock", - "async-net", - "async-process", - "blocking", - "futures-lite", -] +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "smol_str" @@ -1005,6 +854,37 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" +[[package]] +name = "tokio" +version = "1.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8e00990ebabbe4c14c08aca901caed183ecd5c09562a12c824bb53d3c3fd3af" +dependencies = [ + "autocfg", + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.42.0", +] + +[[package]] +name = "tokio-macros" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-bidi" version = "0.3.10" @@ -1066,10 +946,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] -name = "waker-fn" -version = "1.1.0" +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" @@ -1154,15 +1034,6 @@ dependencies = [ "webpki", ] -[[package]] -name = "wepoll-ffi" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d743fdedc5c64377b5fc2bc036b01c7fd642205a0d96356034ae3404d49eb7fb" -dependencies = [ - "cc", -] - [[package]] name = "winapi" version = "0.3.9" @@ -1209,6 +1080,30 @@ dependencies = [ "windows_x86_64_msvc", ] +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.42.1" diff --git a/Cargo.toml b/Cargo.toml index 49a5b89..57b4f68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,10 +28,10 @@ anyhow = "1.0.68" os_str_bytes = "6.4.1" regex = "1.7.1" serde_json = "1.0.91" -smol = "1.3.0" ureq = "2.6.2" clap = { version = "4.1.1", features = ["derive"] } full_moon = { version = "0.17.0", features = ["roblox"] } mlua = { version = "0.8.7", features = ["luau", "async", "serialize"] } serde = { version = "1.0.152", features = ["derive"] } +tokio = { version = "1.24.2", features = ["full"] } diff --git a/src/cli/cli.rs b/src/cli/cli.rs index 643ce73..e2f2c57 100644 --- a/src/cli/cli.rs +++ b/src/cli/cli.rs @@ -4,7 +4,7 @@ use anyhow::Result; use clap::{CommandFactory, Parser}; use lune::Lune; -use smol::fs::{read_to_string, write}; +use tokio::fs::{read_to_string, write}; use crate::{ gen::generate_docs_json_from_definitions, @@ -178,7 +178,7 @@ mod tests { use anyhow::{bail, Context, Result}; use serde_json::Value; - use smol::fs::{create_dir_all, read_to_string, remove_file}; + use tokio::fs::{create_dir_all, read_to_string, remove_file}; use super::{Cli, LUNE_LUAU_FILE_NAME, LUNE_SELENE_FILE_NAME}; @@ -212,29 +212,23 @@ mod tests { } } - #[test] - fn list() -> Result<()> { - smol::block_on(async { - Cli::list().run().await?; - Ok(()) - }) + #[tokio::test] + async fn list() -> Result<()> { + Cli::list().run().await?; + Ok(()) } - #[test] - fn download_selene_types() -> Result<()> { - smol::block_on(async { - run_cli(Cli::download_selene_types()).await?; - ensure_file_exists_and_is_not_json(LUNE_SELENE_FILE_NAME).await?; - Ok(()) - }) + #[tokio::test] + async fn download_selene_types() -> Result<()> { + run_cli(Cli::download_selene_types()).await?; + ensure_file_exists_and_is_not_json(LUNE_SELENE_FILE_NAME).await?; + Ok(()) } - #[test] - fn download_luau_types() -> Result<()> { - smol::block_on(async { - run_cli(Cli::download_luau_types()).await?; - ensure_file_exists_and_is_not_json(LUNE_LUAU_FILE_NAME).await?; - Ok(()) - }) + #[tokio::test] + async fn download_luau_types() -> Result<()> { + run_cli(Cli::download_luau_types()).await?; + ensure_file_exists_and_is_not_json(LUNE_LUAU_FILE_NAME).await?; + Ok(()) } } diff --git a/src/cli/main.rs b/src/cli/main.rs index 98e121d..b1a4759 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -1,9 +1,10 @@ #![deny(clippy::all)] #![warn(clippy::cargo, clippy::pedantic)] #![allow( - clippy::needless_pass_by_value, clippy::match_bool, - clippy::module_name_repetitions + clippy::module_name_repetitions, + clippy::multiple_crate_versions, + clippy::needless_pass_by_value )] use std::process::ExitCode; @@ -17,6 +18,7 @@ mod utils; use cli::Cli; -fn main() -> Result { - smol::block_on(async { Cli::parse().run().await }) +#[tokio::main] +async fn main() -> Result { + Cli::parse().run().await } diff --git a/src/cli/utils/github.rs b/src/cli/utils/github.rs index ab36710..ccbec9b 100644 --- a/src/cli/utils/github.rs +++ b/src/cli/utils/github.rs @@ -4,7 +4,6 @@ use anyhow::{bail, Context, Result}; use serde::{Deserialize, Serialize}; use lune::utils::net::{get_github_owner_and_repo, get_request_user_agent_header}; -use smol::unblock; #[derive(Clone, Deserialize, Serialize)] pub struct ReleaseAsset { @@ -50,7 +49,7 @@ impl Client { for (header, value) in headers { request = request.set(header, value); } - unblock(|| Ok(request.send_string("")?.into_string()?)).await + tokio::task::spawn_blocking(|| Ok(request.send_string("")?.into_string()?)).await? } pub async fn fetch_releases(&self) -> Result> { @@ -82,7 +81,7 @@ impl Client { let file_string = self .get(&asset.url, &[("Accept", "application/octet-stream")]) .await?; - smol::fs::write(&file_path, &file_string) + tokio::fs::write(&file_path, &file_string) .await .with_context(|| { format!("Failed to write file at path '{}'", &file_path.display()) diff --git a/src/cli/utils/listing.rs b/src/cli/utils/listing.rs index 6cbcbff..efa96b4 100644 --- a/src/cli/utils/listing.rs +++ b/src/cli/utils/listing.rs @@ -1,7 +1,7 @@ use std::{cmp::Ordering, fmt::Write as _}; use anyhow::{bail, Result}; -use smol::{fs, io, prelude::*}; +use tokio::{fs, io}; use super::files::parse_lune_description_from_file; @@ -20,7 +20,7 @@ pub async fn find_lune_scripts() -> Result> { match lune_dir { Ok(mut dir) => { let mut files = Vec::new(); - while let Some(entry) = dir.next().await.transpose()? { + while let Some(entry) = dir.next_entry().await? { let meta = entry.metadata().await?; if meta.is_file() { let contents = fs::read_to_string(entry.path()).await?; diff --git a/src/lib/globals/fs.rs b/src/lib/globals/fs.rs index 4ee9652..1b3c978 100644 --- a/src/lib/globals/fs.rs +++ b/src/lib/globals/fs.rs @@ -1,7 +1,7 @@ use std::path::{PathBuf, MAIN_SEPARATOR}; use mlua::prelude::*; -use smol::{fs, prelude::*}; +use tokio::fs; use crate::utils::table::TableBuilder; @@ -28,7 +28,7 @@ async fn fs_read_file(_: &Lua, path: String) -> LuaResult { async fn fs_read_dir(_: &Lua, path: String) -> LuaResult> { let mut dir_strings = Vec::new(); let mut dir = fs::read_dir(&path).await.map_err(LuaError::external)?; - while let Some(dir_entry) = dir.try_next().await.map_err(LuaError::external)? { + while let Some(dir_entry) = dir.next_entry().await.map_err(LuaError::external)? { if let Some(dir_path_str) = dir_entry.path().to_str() { dir_strings.push(dir_path_str.to_owned()); } else { diff --git a/src/lib/globals/process.rs b/src/lib/globals/process.rs index c7b88b1..a795b27 100644 --- a/src/lib/globals/process.rs +++ b/src/lib/globals/process.rs @@ -9,7 +9,7 @@ use std::{ use mlua::prelude::*; use os_str_bytes::RawOsString; -use smol::{channel::Sender, Timer}; +use tokio::{sync::mpsc::Sender, time}; use crate::{ utils::{process::pipe_and_inherit_child_process_stdio, table::TableBuilder}, @@ -129,7 +129,7 @@ async fn process_exit(lua: &Lua, exit_code: Option) -> LuaResult<()> { .map_err(LuaError::external)?; // Make sure to block the rest of this thread indefinitely since // the main thread may not register the exit signal right away - Timer::after(Duration::MAX).await; + time::sleep(Duration::MAX).await; Ok(()) } diff --git a/src/lib/globals/task.rs b/src/lib/globals/task.rs index f17c927..d934555 100644 --- a/src/lib/globals/task.rs +++ b/src/lib/globals/task.rs @@ -4,7 +4,7 @@ use std::{ }; use mlua::prelude::*; -use smol::Timer; +use tokio::time; use crate::utils::{ table::TableBuilder, @@ -132,7 +132,7 @@ async fn task_spawn<'a>( async fn task_wait(lua: &Lua, duration: Option) -> LuaResult { let start = Instant::now(); run_registered_task(lua, TaskRunMode::Blocking, async move { - Timer::after(Duration::from_secs_f32( + time::sleep(Duration::from_secs_f32( duration .map(|d| d.max(MINIMUM_WAIT_OR_DELAY_DURATION)) .unwrap_or(MINIMUM_WAIT_OR_DELAY_DURATION), diff --git a/src/lib/lib.rs b/src/lib/lib.rs index 937c197..7b70377 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -2,7 +2,7 @@ use std::{collections::HashSet, process::ExitCode, sync::Arc}; use anyhow::{bail, Result}; use mlua::prelude::*; -use smol::LocalExecutor; +use tokio::{sync::mpsc, task}; pub mod globals; pub mod utils; @@ -72,15 +72,12 @@ impl Lune { } pub async fn run(&self, name: &str, chunk: &str) -> Result { - let (s, r) = smol::channel::unbounded::(); + let task_set = task::LocalSet::new(); + let (sender, mut receiver) = mpsc::channel::(64); let lua = Arc::new(mlua::Lua::new()); - let exec = Arc::new(LocalExecutor::new()); - let sender = Arc::new(s); - let receiver = Arc::new(r); + let snd = Arc::new(sender); lua.set_app_data(Arc::downgrade(&lua)); - lua.set_app_data(Arc::downgrade(&exec)); - lua.set_app_data(Arc::downgrade(&sender)); - lua.set_app_data(Arc::downgrade(&receiver)); + lua.set_app_data(Arc::downgrade(&snd)); // Add in wanted lune globals for global in &self.globals { match &global { @@ -96,8 +93,9 @@ impl Lune { let script_lua = lua.clone(); let script_name = name.to_string(); let script_chunk = chunk.to_string(); - sender.send(LuneMessage::Spawned).await?; - exec.spawn(async move { + let script_sender = snd.clone(); + script_sender.send(LuneMessage::Spawned).await?; + task_set.spawn_local(async move { let result = script_lua .load(&script_chunk) .set_name(&format!("={}", script_name)) @@ -105,20 +103,19 @@ impl Lune { .eval_async::() .await; match result { - Err(e) => sender.send(LuneMessage::LuaError(e)).await, - Ok(_) => sender.send(LuneMessage::Finished).await, + Err(e) => script_sender.send(LuneMessage::LuaError(e)).await, + Ok(_) => script_sender.send(LuneMessage::Finished).await, } - }) - .detach(); + }); // Run the executor until there are no tasks left, // taking care to not exit right away for errors - let (got_code, got_error, exit_code) = exec - .run(async { + let (got_code, got_error, exit_code) = task_set + .run_until(async { let mut task_count = 0; let mut got_error = false; let mut got_code = false; let mut exit_code = 0; - while let Ok(message) = receiver.recv().await { + while let Some(message) = receiver.recv().await { // Make sure our task-count-modifying messages are sent correctly, one // task spawned must always correspond to one task finished / errored match &message { @@ -174,7 +171,7 @@ mod tests { use std::process::ExitCode; use anyhow::Result; - use smol::fs::read_to_string; + use tokio::fs::read_to_string; use crate::Lune; @@ -183,25 +180,23 @@ mod tests { macro_rules! run_tests { ($($name:ident: $value:expr,)*) => { $( - #[test] - fn $name() -> Result { - smol::block_on(async { - let full_name = format!("src/tests/{}.luau", $value); - let script = read_to_string(&full_name) - .await - .unwrap(); - let lune = Lune::new() - .with_args( - ARGS - .clone() - .iter() - .map(ToString::to_string) - .collect() - ) - .with_all_globals(); - let script_name = full_name.strip_suffix(".luau").unwrap(); - lune.run(&script_name, &script).await - }) + #[tokio::test] + async fn $name() -> Result { + let full_name = format!("src/tests/{}.luau", $value); + let script = read_to_string(&full_name) + .await + .unwrap(); + let lune = Lune::new() + .with_args( + ARGS + .clone() + .iter() + .map(ToString::to_string) + .collect() + ) + .with_all_globals(); + let script_name = full_name.strip_suffix(".luau").unwrap(); + lune.run(&script_name, &script).await } )* } diff --git a/src/lib/utils/task.rs b/src/lib/utils/task.rs index 1da30df..c446a48 100644 --- a/src/lib/utils/task.rs +++ b/src/lib/utils/task.rs @@ -1,9 +1,10 @@ use std::fmt::{self, Debug}; +use std::future::Future; use std::sync::Weak; use mlua::prelude::*; -use smol::{channel::Sender, LocalExecutor}; -use smol::{future::yield_now, prelude::*}; +use tokio::sync::mpsc::Sender; +use tokio::task; use crate::LuneMessage; @@ -29,12 +30,7 @@ pub async fn run_registered_task( mode: TaskRunMode, to_run: impl Future> + 'static, ) -> LuaResult<()> { - // Fetch global references to task executor & message sender - let exec = lua - .app_data_ref::>() - .unwrap() - .upgrade() - .unwrap(); + // Fetch global reference to message sender let sender = lua .app_data_ref::>>() .unwrap() @@ -47,13 +43,13 @@ pub async fn run_registered_task( .map_err(LuaError::external)?; // Run the new task separately from the current one using the executor let sender = sender.clone(); - let task = exec.spawn(async move { + let task = task::spawn_local(async move { // HACK: For deferred tasks we yield a bunch of times to try and ensure // we run our task at the very end of the async queue, this can fail if // the user creates a bunch of interleaved deferred and normal tasks if mode == TaskRunMode::Deferred { for _ in 0..64 { - yield_now().await; + task::yield_now().await; } } sender @@ -64,16 +60,16 @@ pub async fn run_registered_task( }) .await }); - // Wait for the task to complete OR let it run in the background + // Wait for the task to complete if we want this call to be blocking // Any lua errors will be sent through the message channel back // to the main thread which will then handle them properly if mode == TaskRunMode::Blocking { - task.await.map_err(LuaError::external)?; - } else { - task.detach(); + task.await + .map_err(LuaError::external)? + .map_err(LuaError::external)?; } // Yield once right away to let the above spawned task start working // instantly, forcing it to run until completion or until it yields - yield_now().await; + task::yield_now().await; Ok(()) }