Replace std::time::Instant with web_time::Instant (#14668)

# Description
The `std::time::Instant` type panics in the WASM context. To prevent
this, I replaced all uses of `std::time::Instant` in WASM-relevant
crates with `web_time::Instant`. This ensures commands using `Instant`
work in WASM without issues. For non-WASM targets, `web-time` simply
reexports `std::time`, so this change doesn’t affect regular builds
([docs](https://docs.rs/web-time/latest/web_time/)).

To ensure future code doesn't reintroduce `std::time::Instant` in WASM
contexts, I added a `clippy wasm` command to the toolkit. This runs
`cargo clippy` with a `clippy.toml` configured to disallow
`std::time::Instant`. Since `web-time` aliases `std::time` by default,
the `clippy.toml` is stored in `clippy/wasm` and is only loaded when
targeting WASM. I also added a new CI job that tests this too.

# User-Facing Changes

None.
This commit is contained in:
Piepmatz
2024-12-25 09:50:02 +01:00
committed by GitHub
parent c29bcc94e7
commit 4b1f4e63c3
12 changed files with 118 additions and 58 deletions

View File

@ -106,6 +106,7 @@ wax = { workspace = true }
which = { workspace = true, optional = true }
unicode-width = { workspace = true }
data-encoding = { version = "2.6.0", features = ["alloc"] }
web-time = { workspace = true }
[target.'cfg(windows)'.dependencies]
winreg = { workspace = true }

View File

@ -1,6 +1,6 @@
use nu_engine::{command_prelude::*, ClosureEvalOnce};
use nu_protocol::engine::Closure;
use std::time::Instant;
use web_time::Instant;
#[derive(Clone)]
pub struct TimeIt;

View File

@ -20,9 +20,9 @@ use std::{
io::{IsTerminal, Read},
path::PathBuf,
str::FromStr,
time::Instant,
};
use url::Url;
use web_time::Instant;
const STREAM_PAGE_SIZE: usize = 1000;

View File

@ -40,6 +40,7 @@ thiserror = "2.0"
typetag = "0.2"
os_pipe = { workspace = true, optional = true, features = ["io_safety"] }
log = { workspace = true }
web-time = { workspace = true }
[target.'cfg(unix)'.dependencies]
nix = { workspace = true, default-features = false, features = ["signal"] }

View File

@ -10,8 +10,8 @@ use crate::{
ir::IrBlock,
record, PipelineData, ShellError, Span, Value,
};
use std::time::Instant;
use std::{borrow::Borrow, io::BufRead};
use web_time::Instant;
#[derive(Debug, Clone, Copy)]
struct ElementId(usize);

View File

@ -20,6 +20,7 @@ libc = { workspace = true }
log = { workspace = true }
sysinfo = { workspace = true }
itertools = { workspace = true }
web-time = { workspace = true }
[target.'cfg(target_family = "unix")'.dependencies]
nix = { workspace = true, default-features = false, features = ["fs", "term", "process", "signal"] }

View File

@ -18,7 +18,8 @@ use std::ptr;
use std::ptr::null_mut;
use std::sync::LazyLock;
use std::thread;
use std::time::{Duration, Instant};
use std::time::Duration;
use web_time::Instant;
use windows::core::{PCWSTR, PWSTR};