Skip to content

Commit 382af21

Browse files
committed
remove unmaintained instant dep
1 parent 752c26f commit 382af21

File tree

12 files changed

+63
-42
lines changed

12 files changed

+63
-42
lines changed

Cargo.lock

Lines changed: 3 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ dashmap = {version = "5", features = ["serde"]}
2121
ecow = {version = "0.2.2", features = ["serde"]}
2222
enum-iterator = "2.0.0"
2323
indexmap = {version = "2", features = ["serde"]}
24-
instant = "0.1.12"
2524
num_cpus = "1.16.0"
2625
once_cell = "1"
2726
parking_lot = "0.12.1"
@@ -87,6 +86,11 @@ rustfft = {version = "6.2.0", optional = true}
8786
rustls-pemfile = {version = "2.1.2", optional = true}
8887
simple_excel_writer = {version = "0.2.0", optional = true}
8988

89+
# Web-only dependencies
90+
js-sys = {version = "0.3", optional = true}
91+
wasm-bindgen = {version = "0.2.92", optional = true}
92+
web-sys = {version = "0.3.60", optional = true}
93+
9094
[features]
9195
audio = ["hodaun", "lockfree", "audio_encode"]
9296
audio_encode = ["hound"]
@@ -137,6 +141,7 @@ raw_mode = ["rawrrr", "native_sys"]
137141
stand = ["native_sys"]
138142
terminal_image = ["viuer", "image"]
139143
tls = ["httparse", "rustls", "webpki-roots", "rustls-pemfile"]
144+
web = ["wasm-bindgen", "js-sys", "web-sys"]
140145
webcam = ["image", "nokhwa"]
141146
xlsx = ["calamine", "simple_excel_writer"]
142147

site/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ enum-iterator = "2.0.0"
1111
getrandom = {version = "0.2", features = ["js"]}
1212
hound = "3.5.1"
1313
image = "0.24.9"
14-
instant = {version = "0.1.12", features = ['wasm-bindgen']}
1514
js-sys = "0.3"
1615
leptos = "0.6.11"
1716
leptos_meta = {version = "0.6.11", features = ["csr"]}
1817
leptos_router = {version = "0.6.11", features = ["csr"]}
1918
pathdiff = "0.2.1"
2019
serde = {version = "1", features = ["derive"]}
2120
serde_json = "1"
22-
uiua = {path = "..", default-features = false, features = ["batteries"]}
21+
uiua = {path = "..", default-features = false, features = ["batteries", "web"]}
2322
unicode-segmentation = "1.10"
2423
urlencoding = "2"
2524
wasm-bindgen = "0.2.92"
@@ -50,5 +49,6 @@ features = [
5049
"FileList",
5150
"ResizeObserver",
5251
"ResizeObserverEntry",
52+
"Performance",
5353
]
5454
version = "0.3.69"

site/src/backend.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99

1010
use crate::{editor::get_ast_time, weewuh, START_TIME};
1111
use leptos::*;
12-
use uiua::{GitTarget, Handle, Report, SysBackend, EXAMPLE_TXT, EXAMPLE_UA};
12+
use uiua::{now, GitTarget, Handle, Report, SysBackend, EXAMPLE_TXT, EXAMPLE_UA};
1313
use wasm_bindgen::prelude::*;
1414
use wasm_bindgen_futures::JsFuture;
1515
use web_sys::{Request, RequestInit, RequestMode, Response};
@@ -338,7 +338,7 @@ impl SysBackend for WebBackend {
338338
self.play_audio(bytes)
339339
}
340340
fn now(&self) -> f64 {
341-
*START_TIME.get_or_init(|| instant::now() / 1000.0) + instant::now() / 1000.0
341+
*START_TIME.get_or_init(now) + now()
342342
}
343343
fn set_clipboard(&self, contents: &str) -> Result<(), String> {
344344
_ = window()
@@ -349,8 +349,8 @@ impl SysBackend for WebBackend {
349349
Ok(())
350350
}
351351
fn sleep(&self, seconds: f64) -> Result<(), String> {
352-
let start = instant::now();
353-
while (instant::now() - start) / 1000.0 < seconds {}
352+
let start = now();
353+
while (now() - start) / 1000.0 < seconds {}
354354
Ok(())
355355
}
356356
fn load_git_module(&self, url: &str, target: GitTarget) -> Result<PathBuf, String> {

site/src/docs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::{
22
collections::{HashMap, HashSet},
33
iter::once,
4+
time::Duration,
45
};
56

67
use enum_iterator::all;
7-
use instant::Duration;
88
use leptos::{leptos_dom::helpers::location, *};
99
use leptos_meta::*;
1010
use leptos_router::*;

site/src/editor/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use leptos::{
1212
use leptos_router::{use_navigate, BrowserIntegration, History, LocationChange, NavigateOptions};
1313
use uiua::{
1414
format::{format_str, FormatConfig},
15-
is_ident_char, lex, seed_random, Primitive, SysOp, Token,
15+
is_ident_char, lex, now, seed_random, Primitive, SysOp, Token,
1616
};
1717
use wasm_bindgen::{closure::Closure, JsCast, JsValue};
1818
use web_sys::{
@@ -187,7 +187,7 @@ pub fn Editor<'a>(
187187
update_token_count(&code_text);
188188

189189
// Format code
190-
let seed = instant::now().to_bits();
190+
let seed = now().to_bits();
191191
seed_random(seed);
192192
let input = if format {
193193
if let Ok(formatted) = format_str(

site/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ mod tour;
1313
mod tutorial;
1414
mod uiuisms;
1515

16-
use std::sync::OnceLock;
16+
use std::{sync::OnceLock, time::Duration};
1717

1818
use base64::engine::{general_purpose::URL_SAFE, Engine};
19-
use instant::Duration;
2019
use js_sys::Date;
2120
use leptos::*;
2221
use leptos_meta::*;
2322
use leptos_router::*;
2423
use uiua::{
2524
lsp::{BindingDocs, BindingDocsKind},
26-
ConstantDef, PrimClass, Primitive, Signature, SysOp,
25+
now, ConstantDef, PrimClass, Primitive, Signature, SysOp,
2726
};
2827
use wasm_bindgen::JsCast;
2928
use web_sys::{Element, HtmlAudioElement};
@@ -195,7 +194,7 @@ pub fn Site() -> impl IntoView {
195194
}
196195

197196
fn weewuh() {
198-
let i = (instant::now() / 1000.0 % 1.0 * 100.0) as u32;
197+
let i = (now() / 1000.0 % 1.0 * 100.0) as u32;
199198
let src = match i {
200199
0 => "/assets/ooh-ee-ooh-ah.mp3",
201200
1..=4 => "/assets/wee-wah.mp3",

src/compile/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use std::{
1313
path::{Path, PathBuf},
1414
rc::Rc,
1515
sync::Arc,
16+
time::Duration,
1617
};
1718

1819
use ecow::{eco_vec, EcoString, EcoVec};
1920
use indexmap::IndexMap;
20-
use instant::Duration;
2121
use serde::{Deserialize, Serialize};
2222

2323
use crate::{

src/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use std::{
88
fs,
99
iter::repeat,
1010
path::{Path, PathBuf},
11+
time::Duration,
1112
};
1213

13-
use instant::Duration;
1414
use paste::paste;
1515

1616
use crate::{

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ use std::{
88
process::{exit, Child, Command, Stdio},
99
sync::mpsc::channel,
1010
thread::sleep,
11-
time::Duration,
11+
time::{Duration, Instant},
1212
};
1313

1414
use clap::{error::ErrorKind, Parser};
1515
use colored::*;
16-
use instant::Instant;
1716
use notify::{EventKind, RecursiveMode, Watcher};
1817
use once_cell::sync::Lazy;
1918
use parking_lot::Mutex;

src/run.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use std::{
1414
atomic::{self, AtomicBool},
1515
Arc,
1616
},
17+
time::Duration,
1718
};
1819

1920
use crossbeam_channel::{Receiver, Sender, TryRecvError};
2021
use enum_iterator::{all, Sequence};
21-
use instant::Duration;
2222
use thread_local::ThreadLocal;
2323

2424
use crate::{
@@ -270,7 +270,7 @@ impl Uiua {
270270
}
271271
/// Limit the execution duration
272272
pub fn with_execution_limit(mut self, limit: Duration) -> Self {
273-
self.rt.execution_limit = Some(limit.as_millis() as f64);
273+
self.rt.execution_limit = Some(limit.as_secs_f64());
274274
self
275275
}
276276
/// Set the command line arguments
@@ -337,7 +337,7 @@ impl Uiua {
337337
pub fn run_asm(&mut self, asm: impl Into<Assembly>) -> UiuaResult {
338338
fn run_asm(env: &mut Uiua, asm: Assembly) -> UiuaResult {
339339
env.asm = asm;
340-
env.rt.execution_start = instant::now();
340+
env.rt.execution_start = env.rt.backend.now();
341341
let res = env.run_top_slices();
342342
if res.is_err() {
343343
env.rt = Runtime {
@@ -431,16 +431,16 @@ code:
431431

432432
if self.rt.time_instrs {
433433
formatted_instr = format!("{instr:?}");
434-
self.rt.last_time = instant::now();
434+
self.rt.last_time = self.rt.backend.now();
435435
}
436436
let res = match instr {
437437
Instr::Comment(_) => Ok(()),
438438
// Pause execution timer during &sc
439439
&Instr::Prim(prim @ Primitive::Sys(SysOp::ScanLine), span) => {
440440
self.with_prim_span(span, Some(prim), |env| {
441-
let start = instant::now();
441+
let start = env.rt.backend.now();
442442
let res = prim.run(env);
443-
env.rt.execution_start += instant::now() - start;
443+
env.rt.execution_start += env.rt.backend.now() - start;
444444
res
445445
})
446446
}
@@ -664,7 +664,7 @@ code:
664664
}
665665
};
666666
if self.rt.time_instrs {
667-
let end_time = instant::now();
667+
let end_time = self.rt.backend.now();
668668
let padding = self.rt.call_stack.len().saturating_sub(1) * 2;
669669
#[rustfmt::skip]
670670
println!( // Allow println
@@ -673,7 +673,7 @@ code:
673673
end_time - self.rt.last_time,
674674
formatted_instr
675675
);
676-
self.rt.last_time = instant::now();
676+
self.rt.last_time = self.rt.backend.now();
677677
}
678678
if let Err(mut err) = res {
679679
// Trace errors
@@ -695,7 +695,7 @@ code:
695695
/// Timeout if an execution limit is set and has been exceeded
696696
pub fn respect_execution_limit(&self) -> UiuaResult {
697697
if let Some(limit) = self.rt.execution_limit {
698-
if instant::now() - self.rt.execution_start > limit {
698+
if self.rt.backend.now() - self.rt.execution_start > limit {
699699
return Err(
700700
UiuaErrorKind::Timeout(self.span(), self.inputs().clone().into()).into(),
701701
);

src/sys.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ pub trait SysBackend: Any + Send + Sync + 'static {
923923
///
924924
/// Should be in seconds
925925
fn now(&self) -> f64 {
926-
instant::now() / 1000.0
926+
now()
927927
}
928928
/// Create a TCP listener and bind it to an address
929929
fn tcp_listen(&self, addr: &str) -> Result<Handle, String> {
@@ -2452,3 +2452,32 @@ pub fn gif_bytes_to_value(bytes: &[u8]) -> Result<(f64, Value), gif::DecodingErr
24522452
num.compress();
24532453
Ok((frame_rate, num))
24542454
}
2455+
2456+
/// Get the current time in seconds
2457+
///
2458+
/// This function works on both native and web targets.
2459+
pub fn now() -> f64 {
2460+
#[cfg(not(target_arch = "wasm32"))]
2461+
{
2462+
std::time::SystemTime::now()
2463+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
2464+
.expect("System clock was before 1970.")
2465+
.as_secs_f64()
2466+
}
2467+
#[cfg(target_arch = "wasm32")]
2468+
{
2469+
#[cfg(not(feature = "web"))]
2470+
{
2471+
compile_error!("Web target requires the `web` feature")
2472+
}
2473+
#[cfg(feature = "web")]
2474+
{
2475+
use wasm_bindgen::{prelude::*, JsCast};
2476+
js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("performance"))
2477+
.expect("failed to get performance from global object")
2478+
.unchecked_into::<web_sys::Performance>()
2479+
.now()
2480+
/ 1000.0
2481+
}
2482+
}
2483+
}

0 commit comments

Comments
 (0)