Skip to content

Commit

Permalink
Leverage Rust 1.79, 1.80 (#9498)
Browse files Browse the repository at this point in the history
This commit is a follow-up to #9496 to leverage various APIs that the
workspace now has access to. For example most dependencies on the
`once_cell` crate are now removed in favor of the types stabilized in
the standard library: `LazyLock` and `LazyCell`. One dependency remains
in the `wasmtime` crate due to the `get_or_try_init` not being stable
yet.

Some additional helper methods on raw pointer slices are also available
for removing a few minor `unsafe` blocks.
  • Loading branch information
alexcrichton authored Oct 22, 2024
1 parent a40776c commit b79a96a
Show file tree
Hide file tree
Showing 32 changed files with 46 additions and 73 deletions.
9 changes: 0 additions & 9 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ clap = { workspace = true }
clap_complete = { workspace = true, optional = true }
anyhow = { workspace = true, features = ['std'] }
target-lexicon = { workspace = true }
once_cell = { workspace = true }
listenfd = { version = "1.0.0", optional = true }
wat = { workspace = true, optional = true }
serde = { workspace = true }
Expand Down Expand Up @@ -299,7 +298,6 @@ clap = { version = "4.5.17", default-features = false, features = ["std", "deriv
clap_complete = "4.4.7"
hashbrown = { version = "0.14", default-features = false }
capstone = "0.12.0"
once_cell = { version = "1.12.0", default-features = false }
smallvec = { version = "1.6.1", features = ["union"] }
tracing = "0.1.26"
bitflags = "2.0"
Expand Down
8 changes: 4 additions & 4 deletions benches/instantiation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use once_cell::unsync::Lazy;
use std::cell::LazyCell;
use std::path::Path;
use std::process::Command;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
Expand Down Expand Up @@ -35,7 +35,7 @@ fn bench_sequential(c: &mut Criterion, path: &Path) {
benchmark_name(&strategy),
path.file_name().unwrap().to_str().unwrap(),
);
let state = Lazy::new(|| {
let state = LazyCell::new(|| {
let mut config = Config::default();
config.allocation_strategy(strategy.clone());

Expand Down Expand Up @@ -70,7 +70,7 @@ fn bench_parallel(c: &mut Criterion, path: &Path) {
let mut group = c.benchmark_group("parallel");

for strategy in strategies() {
let state = Lazy::new(|| {
let state = LazyCell::new(|| {
let mut config = Config::default();
config.allocation_strategy(strategy.clone());

Expand Down Expand Up @@ -153,7 +153,7 @@ fn bench_deserialize_module(c: &mut Criterion, path: &Path) {

let name = path.file_name().unwrap().to_str().unwrap();
let tmpfile = tempfile::NamedTempFile::new().unwrap();
let state = Lazy::new(|| {
let state = LazyCell::new(|| {
let engine = Engine::default();
let module = Module::from_file(&engine, path).expect("failed to load WASI example module");
let bytes = module.serialize().unwrap();
Expand Down
3 changes: 1 addition & 2 deletions cranelift/fuzzgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ workspace = true
cranelift = { workspace = true }
cranelift-native = { workspace = true }

anyhow = { workspace = true }
anyhow = { workspace = true, features = ['std'] }
arbitrary = { workspace = true }
once_cell = { workspace = true }
target-lexicon = { workspace = true, features = ["std"] }
4 changes: 2 additions & 2 deletions cranelift/fuzzgen/src/function_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use cranelift::prelude::{
EntityRef, ExtFuncData, FloatCC, InstBuilder, IntCC, JumpTableData, MemFlags, StackSlotData,
StackSlotKind,
};
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::ops::RangeInclusive;
use std::str::FromStr;
use std::sync::LazyLock;
use target_lexicon::{Architecture, Triple};

type BlockSignature = Vec<Type>;
Expand Down Expand Up @@ -789,7 +789,7 @@ fn valid_for_target(triple: &Triple, op: Opcode, args: &[Type], rets: &[Type]) -

type OpcodeSignature = (Opcode, Vec<Type>, Vec<Type>);

static OPCODE_SIGNATURES: Lazy<Vec<OpcodeSignature>> = Lazy::new(|| {
static OPCODE_SIGNATURES: LazyLock<Vec<OpcodeSignature>> = LazyLock::new(|| {
let types = &[
I8, I16, I32, I64, I128, // Scalar Integers
F32, F64, // Scalar Floats
Expand Down
1 change: 0 additions & 1 deletion crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ doctest = false
[dependencies]
env_logger = { workspace = true, optional = true }
anyhow = { workspace = true }
once_cell = { workspace = true }
wasmtime = { workspace = true, features = ['runtime', 'gc', 'std'] }
wasmtime-c-api-macros = { workspace = true }
log = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/trap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{wasm_frame_vec_t, wasm_instance_t, wasm_name_t, wasm_store_t};
use anyhow::{anyhow, Error};
use once_cell::unsync::OnceCell;
use std::cell::OnceCell;
use wasmtime::{Trap, WasmBacktrace};

#[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/types/export.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{wasm_externtype_t, wasm_name_t, CExternType};
use once_cell::unsync::OnceCell;
use std::cell::OnceCell;

#[repr(C)]
#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/types/func.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{wasm_externtype_t, wasm_valtype_t, wasm_valtype_vec_t, CExternType};
use once_cell::unsync::OnceCell;
use std::cell::OnceCell;
use std::{
mem,
sync::{Arc, Mutex},
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/types/global.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{wasm_externtype_t, wasm_valtype_t, CExternType};
use once_cell::unsync::OnceCell;
use std::cell::OnceCell;
use wasmtime::GlobalType;

pub type wasm_mutability_t = u8;
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/types/import.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{wasm_externtype_t, wasm_name_t, CExternType};
use once_cell::unsync::OnceCell;
use std::cell::OnceCell;

#[repr(C)]
#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/types/memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{wasm_externtype_t, wasm_limits_t, CExternType};
use once_cell::unsync::OnceCell;
use std::cell::OnceCell;
use std::convert::TryFrom;
use wasmtime::MemoryType;

Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/types/table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{wasm_externtype_t, wasm_limits_t, wasm_valtype_t, CExternType};
use once_cell::unsync::OnceCell;
use std::cell::OnceCell;
use wasmtime::{TableType, ValType};

#[repr(transparent)]
Expand Down
1 change: 0 additions & 1 deletion crates/cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ rustix = { workspace = true, features = ["process"] }

[dev-dependencies]
filetime = "0.2.7"
once_cell = { workspace = true }
pretty_env_logger = { workspace = true }
tempfile = "3"
4 changes: 2 additions & 2 deletions crates/cache/src/worker/tests/system_time_stub.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use once_cell::sync::Lazy;
use std::sync::LazyLock;
use std::time::{Duration, SystemTime, SystemTimeError};

pub static NOW: Lazy<SystemTime> = Lazy::new(SystemTime::now);
pub static NOW: LazyLock<SystemTime> = LazyLock::new(SystemTime::now);

#[derive(PartialOrd, PartialEq, Ord, Eq)]
pub struct SystemTimeStub(SystemTime);
Expand Down
3 changes: 1 addition & 2 deletions crates/fuzzing/wasm-spec-interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ workspace = true
# `build-libinterpret` feature set by this crate's parent).
[dependencies]
ocaml-interop = { version = "0.8", optional = true }
once_cell = { workspace = true, optional = true }

[dev-dependencies]
wat = { workspace = true }

[features]
build-libinterpret = ["ocaml-interop", "once_cell"]
build-libinterpret = ["ocaml-interop"]
3 changes: 1 addition & 2 deletions crates/fuzzing/wasm-spec-interpreter/src/with_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@

use crate::{SpecExport, SpecInstance, SpecValue};
use ocaml_interop::{BoxRoot, OCamlRuntime, ToOCaml};
use once_cell::sync::Lazy;
use std::sync::Mutex;

static INTERPRET: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
static INTERPRET: Mutex<()> = Mutex::new(());

/// Instantiate the WebAssembly module in the spec interpreter.
pub fn instantiate(module: &[u8]) -> Result<SpecInstance, String> {
Expand Down
3 changes: 1 addition & 2 deletions crates/jit-debug/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ rust-version.workspace = true
workspace = true

[dependencies]
once_cell = { workspace = true, optional = true }
object = { workspace = true, optional = true }
wasmtime-versioned-export-macros = { workspace = true }

[target.'cfg(target_os = "linux")'.dependencies]
rustix = { workspace = true, features = ["mm", "param", "time"], optional = true }

[features]
gdb_jit_int = ["once_cell"]
gdb_jit_int = []
perf_jitdump = ["rustix", "object"]
3 changes: 1 addition & 2 deletions crates/jit-debug/src/gdb_jit_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! the __jit_debug_register_code() and __jit_debug_descriptor to register
//! or unregister generated object images with debuggers.

use once_cell::sync::Lazy;
use std::pin::Pin;
use std::ptr;
use std::sync::Mutex;
Expand Down Expand Up @@ -40,7 +39,7 @@ extern "C" {
///
/// The GDB_REGISTRATION lock is needed for GdbJitImageRegistration to protect
/// access to the __jit_debug_descriptor within this process.
static GDB_REGISTRATION: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(Default::default()));
static GDB_REGISTRATION: Mutex<()> = Mutex::new(());

/// Registration for JIT image
pub struct GdbJitImageRegistration {
Expand Down
1 change: 0 additions & 1 deletion crates/wasi-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ libc = { workspace = true, optional = true }
rustix = { workspace = true, features = ["fs", "event"] }

[target.'cfg(windows)'.dependencies]
once_cell = { workspace = true }
io-extras = { workspace = true }
rustix = { workspace = true, features = ["net"] }

Expand Down
5 changes: 2 additions & 3 deletions crates/wasi-common/src/sync/sched/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

use crate::sched::subscription::{RwEventFlags, Subscription};
use crate::{file::WasiFile, sched::Poll, Error, ErrorExt};
use once_cell::sync::Lazy;
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError};
use std::sync::Mutex;
use std::sync::{LazyLock, Mutex};
use std::thread;
use std::time::Duration;

Expand Down Expand Up @@ -144,7 +143,7 @@ struct StdinPoll {
notify_rx: Receiver<PollState>,
}

static STDIN_POLL: Lazy<Mutex<StdinPoll>> = Lazy::new(StdinPoll::new);
static STDIN_POLL: LazyLock<Mutex<StdinPoll>> = LazyLock::new(StdinPoll::new);

impl StdinPoll {
pub fn new() -> Mutex<Self> {
Expand Down
1 change: 0 additions & 1 deletion crates/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ async-trait = { workspace = true }
system-interface = { workspace = true}
futures = { workspace = true }
url = { workspace = true }
once_cell = { workspace = true }

[dev-dependencies]
tokio = { workspace = true, features = ["time", "sync", "io-std", "io-util", "rt", "rt-multi-thread", "net", "macros", "fs"] }
Expand Down
16 changes: 8 additions & 8 deletions crates/wasi/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

use std::future::Future;
use std::pin::Pin;
use std::sync::LazyLock;
use std::task::{Context, Poll};

pub(crate) static RUNTIME: once_cell::sync::Lazy<tokio::runtime::Runtime> =
once_cell::sync::Lazy::new(|| {
tokio::runtime::Builder::new_multi_thread()
.enable_time()
.enable_io()
.build()
.unwrap()
});
pub(crate) static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new(|| {
tokio::runtime::Builder::new_multi_thread()
.enable_time()
.enable_io()
.build()
.unwrap()
});

/// Exactly like a [`tokio::task::JoinHandle`], except that it aborts the task when
/// the handle is dropped.
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ sptr = { workspace = true }
postcard = { workspace = true }
indexmap = { workspace = true }
paste = "1.0.3"
once_cell = { workspace = true }
once_cell = { version = "1.12.0", optional = true }
rayon = { version = "1.0", optional = true }
object = { workspace = true }
async-trait = { workspace = true, optional = true }
Expand Down Expand Up @@ -267,7 +267,7 @@ std = [
'wasmtime-component-macro?/std',
'wasmtime-environ/std',
'object/std',
'once_cell/std',
'once_cell',
]

# Enables support for the `Store::call_hook` API which enables injecting custom
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmtime/src/runtime/vm/sys/custom/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Mmap {

#[inline]
pub fn len(&self) -> usize {
unsafe { (*self.memory.as_ptr()).len() }
self.memory.as_ptr().len()
}

pub unsafe fn make_executable(
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Drop for Mmap {
fn drop(&mut self) {
unsafe {
let ptr = self.memory.as_ptr().cast();
let len = (*self.memory.as_ptr()).len();
let len = self.memory.as_ptr().len();
if len == 0 {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/vm/sys/miri/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Mmap {
}

pub fn len(&self) -> usize {
unsafe { (*self.memory.as_ptr()).len() }
self.memory.as_ptr().len()
}

pub unsafe fn make_executable(
Expand Down
Loading

0 comments on commit b79a96a

Please sign in to comment.