From 53e8316c7f41b19daf55edbe8976af603d293382 Mon Sep 17 00:00:00 2001 From: Yang Hau Date: Fri, 2 Jun 2023 18:58:25 -0500 Subject: [PATCH] refactor: Replace lazy-static with LazyLock --- Cargo.lock | 1 - crates/test-programs/Cargo.toml | 1 - crates/test-programs/build.rs | 16 +++++-------- crates/test-programs/tests/command.rs | 23 ++++++++++--------- crates/test-programs/tests/reactor.rs | 20 ++++++++-------- .../test-programs/tests/wasi-cap-std-sync.rs | 20 ++++++++-------- crates/test-programs/tests/wasi-http.rs | 17 +++++++------- .../tests/wasi-preview1-host-in-preview2.rs | 20 ++++++++-------- .../tests/wasi-preview2-components.rs | 20 ++++++++-------- crates/test-programs/tests/wasi-tokio.rs | 20 ++++++++-------- tests/spec_testsuite | 2 +- 11 files changed, 76 insertions(+), 84 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b170583ffb87..77cc1cf25ae0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3253,7 +3253,6 @@ dependencies = [ "http-body", "http-body-util", "hyper", - "lazy_static", "tempfile", "test-log", "tokio", diff --git a/crates/test-programs/Cargo.toml b/crates/test-programs/Cargo.toml index 37a97b40b384..f7ffc257fe13 100644 --- a/crates/test-programs/Cargo.toml +++ b/crates/test-programs/Cargo.toml @@ -20,7 +20,6 @@ tempfile = { workspace = true } test-log = { version = "0.2", default-features = false, features = ["trace"] } tracing = { workspace = true } tracing-subscriber = { version = "0.3.1", default-features = false, features = ['fmt', 'env-filter'] } -lazy_static = "1" wasmtime = { workspace = true, features = ['cranelift', 'component-model'] } wasi-common = { workspace = true } diff --git a/crates/test-programs/build.rs b/crates/test-programs/build.rs index eccf8e0d6e00..7e9151192866 100644 --- a/crates/test-programs/build.rs +++ b/crates/test-programs/build.rs @@ -98,11 +98,9 @@ fn modules_rs(meta: &cargo_metadata::Metadata, package: &str, kind: &str, out_di // Load the module from disk only once, in case it is used many times: decls += &format!( " - lazy_static::lazy_static!{{ - static ref {global}: wasmtime::Module = {{ - wasmtime::Module::from_file(&ENGINE, {file:?}).unwrap() - }}; - }} + static {global}: std::sync::LazyLock = std::sync::LazyLock::new(|| {{ + wasmtime::Module::from_file(&ENGINE, {file:?}).unwrap() + }}); " ); // Match the stem str literal to the module. Cloning is just a ref count incr. @@ -179,11 +177,9 @@ fn components_rs( let global = format!("{}_COMPONENT", stem.to_uppercase()); decls += &format!( " - lazy_static::lazy_static!{{ - static ref {global}: wasmtime::component::Component = {{ - wasmtime::component::Component::from_file(&ENGINE, {file:?}).unwrap() - }}; - }} + static {global}: std::sync::LazyLock = std::sync::LazyLock::new(|| {{ + wasmtime::component::Component::from_file(&ENGINE, {file:?}).unwrap() + }}); " ); cases += &format!("{stem:?} => {global}.clone(),\n"); diff --git a/crates/test-programs/tests/command.rs b/crates/test-programs/tests/command.rs index 3473a7c9ddba..070c391e7f09 100644 --- a/crates/test-programs/tests/command.rs +++ b/crates/test-programs/tests/command.rs @@ -1,8 +1,10 @@ +#![feature(lazy_cell)] + use anyhow::Result; use cap_std::{ambient_authority, fs::Dir, time::Duration}; use std::{ io::{Cursor, Write}, - sync::Mutex, + sync::{LazyLock, Mutex}, }; use wasmtime::{ component::{Component, Linker}, @@ -16,17 +18,16 @@ use wasmtime_wasi::preview2::{ DirPerms, FilePerms, Table, WasiCtx, WasiCtxBuilder, WasiView, }; -lazy_static::lazy_static! { - static ref ENGINE: Engine = { - let mut config = Config::new(); - config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); - config.wasm_component_model(true); - config.async_support(true); +static ENGINE: LazyLock = LazyLock::new(|| { + let mut config = Config::new(); + config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); + config.wasm_component_model(true); + config.async_support(true); + + let engine = Engine::new(&config).unwrap(); + engine +}); - let engine = Engine::new(&config).unwrap(); - engine - }; -} // uses ENGINE, creates a fn get_component(&str) -> Component include!(concat!(env!("OUT_DIR"), "/command_tests_components.rs")); diff --git a/crates/test-programs/tests/reactor.rs b/crates/test-programs/tests/reactor.rs index 7091aafc0e34..812a8327e4df 100644 --- a/crates/test-programs/tests/reactor.rs +++ b/crates/test-programs/tests/reactor.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use std::sync::{Arc, RwLock}; +use std::sync::{Arc, LazyLock, RwLock}; use wasmtime::{ component::{Component, Linker}, Config, Engine, Store, @@ -8,17 +8,15 @@ use wasmtime_wasi::preview2::wasi::clocks::wall_clock; use wasmtime_wasi::preview2::wasi::filesystem::filesystem; use wasmtime_wasi::preview2::{self, Table, WasiCtx, WasiCtxBuilder, WasiView}; -lazy_static::lazy_static! { - static ref ENGINE: Engine = { - let mut config = Config::new(); - config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); - config.wasm_component_model(true); - config.async_support(true); +static ENGINE: LazyLock = LazyLock::new(|| { + let mut config = Config::new(); + config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); + config.wasm_component_model(true); + config.async_support(true); - let engine = Engine::new(&config).unwrap(); - engine - }; -} + let engine = Engine::new(&config).unwrap(); + engine +}); // uses ENGINE, creates a fn get_component(&str) -> Component include!(concat!(env!("OUT_DIR"), "/reactor_tests_components.rs")); diff --git a/crates/test-programs/tests/wasi-cap-std-sync.rs b/crates/test-programs/tests/wasi-cap-std-sync.rs index 3e1ba3d15cce..f2a9c5449f35 100644 --- a/crates/test-programs/tests/wasi-cap-std-sync.rs +++ b/crates/test-programs/tests/wasi-cap-std-sync.rs @@ -1,20 +1,20 @@ #![cfg(feature = "test_programs")] use anyhow::Result; +use std::sync::LazyLock; use tempfile::TempDir; use wasi_common::pipe::WritePipe; use wasmtime::{Config, Engine, Linker, Store}; -lazy_static::lazy_static! { - static ref ENGINE: Engine = { - let mut config = Config::new(); - config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); - config.wasm_component_model(false); - config.async_support(false); +static ENGINE: LazyLock = LazyLock::new(|| { + let mut config = Config::new(); + config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); + config.wasm_component_model(false); + config.async_support(false); + + let engine = Engine::new(&config).unwrap(); + engine +}); - let engine = Engine::new(&config).unwrap(); - engine - }; -} // uses ENGINE, creates a fn get_module(&str) -> Module include!(concat!(env!("OUT_DIR"), "/wasi_tests_modules.rs")); diff --git a/crates/test-programs/tests/wasi-http.rs b/crates/test-programs/tests/wasi-http.rs index 0b1a4fd70567..69abc33222d4 100644 --- a/crates/test-programs/tests/wasi-http.rs +++ b/crates/test-programs/tests/wasi-http.rs @@ -7,17 +7,16 @@ use http_body_util::combinators::BoxBody; use http_body_util::BodyExt; use hyper::server::conn::http1; use hyper::{body::Bytes, service::service_fn, Request, Response}; -use std::{error::Error, net::SocketAddr}; +use std::{error::Error, net::SocketAddr, sync::LazyLock}; use tokio::net::TcpListener; -lazy_static::lazy_static! { - static ref ENGINE: Engine = { - let mut config = Config::new(); - config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); - let engine = Engine::new(&config).unwrap(); - engine - }; -} +static ENGINE: LazyLock = LazyLock::new(|| { + let mut config = Config::new(); + config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); + let engine = Engine::new(&config).unwrap(); + engine +}); + // uses ENGINE, creates a fn get_module(&str) -> Module include!(concat!(env!("OUT_DIR"), "/wasi_http_tests_modules.rs")); diff --git a/crates/test-programs/tests/wasi-preview1-host-in-preview2.rs b/crates/test-programs/tests/wasi-preview1-host-in-preview2.rs index 0776cf1017a5..0323180e16ae 100644 --- a/crates/test-programs/tests/wasi-preview1-host-in-preview2.rs +++ b/crates/test-programs/tests/wasi-preview1-host-in-preview2.rs @@ -1,5 +1,6 @@ #![cfg(feature = "test_programs")] use anyhow::Result; +use std::sync::LazyLock; use tempfile::TempDir; use wasmtime::{Config, Engine, Linker, Store}; use wasmtime_wasi::preview2::{ @@ -8,17 +9,16 @@ use wasmtime_wasi::preview2::{ DirPerms, FilePerms, Table, WasiCtx, WasiCtxBuilder, WasiView, }; -lazy_static::lazy_static! { - static ref ENGINE: Engine = { - let mut config = Config::new(); - config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); - config.wasm_component_model(true); - config.async_support(true); +static ENGINE: LazyLock = LazyLock::new(|| { + let mut config = Config::new(); + config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); + config.wasm_component_model(true); + config.async_support(true); + + let engine = Engine::new(&config).unwrap(); + engine +}); - let engine = Engine::new(&config).unwrap(); - engine - }; -} // uses ENGINE, creates a fn get_module(&str) -> Module include!(concat!(env!("OUT_DIR"), "/wasi_tests_modules.rs")); diff --git a/crates/test-programs/tests/wasi-preview2-components.rs b/crates/test-programs/tests/wasi-preview2-components.rs index 027ca510cec9..7deb499615e3 100644 --- a/crates/test-programs/tests/wasi-preview2-components.rs +++ b/crates/test-programs/tests/wasi-preview2-components.rs @@ -1,5 +1,6 @@ #![cfg(feature = "test_programs")] use anyhow::Result; +use std::sync::LazyLock; use tempfile::TempDir; use wasmtime::{component::Linker, Config, Engine, Store}; use wasmtime_wasi::preview2::{ @@ -8,17 +9,16 @@ use wasmtime_wasi::preview2::{ DirPerms, FilePerms, Table, WasiCtx, WasiCtxBuilder, WasiView, }; -lazy_static::lazy_static! { - static ref ENGINE: Engine = { - let mut config = Config::new(); - config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); - config.wasm_component_model(true); - config.async_support(true); +static ENGINE: LazyLock = LazyLock::new(|| { + let mut config = Config::new(); + config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); + config.wasm_component_model(true); + config.async_support(true); + + let engine = Engine::new(&config).unwrap(); + engine +}); - let engine = Engine::new(&config).unwrap(); - engine - }; -} // uses ENGINE, creates a fn get_component(&str) -> Component include!(concat!(env!("OUT_DIR"), "/wasi_tests_components.rs")); diff --git a/crates/test-programs/tests/wasi-tokio.rs b/crates/test-programs/tests/wasi-tokio.rs index f14f273d0f2c..1ae32d722452 100644 --- a/crates/test-programs/tests/wasi-tokio.rs +++ b/crates/test-programs/tests/wasi-tokio.rs @@ -1,21 +1,21 @@ #![cfg(feature = "test_programs")] use anyhow::Result; +use std::sync::LazyLock; use tempfile::TempDir; use wasi_common::pipe::WritePipe; use wasmtime::{Config, Engine, Linker, Store}; use wasmtime_wasi::tokio::{add_to_linker, WasiCtxBuilder}; -lazy_static::lazy_static! { - static ref ENGINE: Engine = { - let mut config = Config::new(); - config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); - config.wasm_component_model(false); - config.async_support(true); +static ENGINE: LazyLock = LazyLock::new(|| { + let mut config = Config::new(); + config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); + config.wasm_component_model(false); + config.async_support(true); + + let engine = Engine::new(&config).unwrap(); + engine +}); - let engine = Engine::new(&config).unwrap(); - engine - }; -} // uses ENGINE, creates a fn get_module(&str) -> Module include!(concat!(env!("OUT_DIR"), "/wasi_tests_modules.rs")); diff --git a/tests/spec_testsuite b/tests/spec_testsuite index 7ef86ddeed81..e25ae159357c 160000 --- a/tests/spec_testsuite +++ b/tests/spec_testsuite @@ -1 +1 @@ -Subproject commit 7ef86ddeed81458f9031a49a40b3a3f99c1c6a8a +Subproject commit e25ae159357c055b3a6fac99043644e208d26d2a