Skip to content

Commit

Permalink
refactor: Replace lazy-static with LazyLock
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Jul 18, 2023
1 parent 61a343b commit 53e8316
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 84 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/test-programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
16 changes: 6 additions & 10 deletions crates/test-programs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<wasmtime::Module> = 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.
Expand Down Expand Up @@ -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<wasmtime::component::Component> = std::sync::LazyLock::new(|| {{
wasmtime::component::Component::from_file(&ENGINE, {file:?}).unwrap()
}});
"
);
cases += &format!("{stem:?} => {global}.clone(),\n");
Expand Down
23 changes: 12 additions & 11 deletions crates/test-programs/tests/command.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<Engine> = 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"));

Expand Down
20 changes: 9 additions & 11 deletions crates/test-programs/tests/reactor.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Engine> = 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"));
Expand Down
20 changes: 10 additions & 10 deletions crates/test-programs/tests/wasi-cap-std-sync.rs
Original file line number Diff line number Diff line change
@@ -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<Engine> = 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"));

Expand Down
17 changes: 8 additions & 9 deletions crates/test-programs/tests/wasi-http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Engine> = 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"));

Expand Down
20 changes: 10 additions & 10 deletions crates/test-programs/tests/wasi-preview1-host-in-preview2.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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<Engine> = 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"));

Expand Down
20 changes: 10 additions & 10 deletions crates/test-programs/tests/wasi-preview2-components.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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<Engine> = 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"));

Expand Down
20 changes: 10 additions & 10 deletions crates/test-programs/tests/wasi-tokio.rs
Original file line number Diff line number Diff line change
@@ -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<Engine> = 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"));

Expand Down
2 changes: 1 addition & 1 deletion tests/spec_testsuite
Submodule spec_testsuite updated 122 files

0 comments on commit 53e8316

Please sign in to comment.