ethdos-fold/src/utils.rs
src/utils.rsRaw
1#[cfg(target_arch = "wasm32")]
2use web_sys::console;
3
4pub fn set_panic_hook() {
5 // When the `console_error_panic_hook` feature is enabled, we can call the
6 // `set_panic_hook` function at least once during initialization, and then
7 // we will get better error messages if our code ever panics.
8 //
9 // For more details see
10 // https://github.com/rustwasm/console_error_panic_hook#readme
11 #[cfg(feature = "console_error_panic_hook")]
12 console_error_panic_hook::set_once();
13}
14
15pub fn dbg(s: String) {
16 #[cfg(target_arch = "wasm32")]
17 console::log_1(&s.into());
18
19 #[cfg(not(target_arch = "wasm32"))]
20 println!("{}", s);
21}
22
23pub fn get_time() -> u64 {
24 #[cfg(target_arch = "wasm32")]
25 let start = get_wasm_time() as u64;
26
27 #[cfg(not(target_arch = "wasm32"))]
28 let start = std::time::SystemTime::now()
29 .duration_since(std::time::UNIX_EPOCH)
30 .unwrap()
31 .as_millis() as u64;
32
33 start
34}
35
36pub fn elapsed(start: u64) -> u64 {
37 #[cfg(target_arch = "wasm32")]
38 let end = get_wasm_time() as u64;
39
40 #[cfg(not(target_arch = "wasm32"))]
41 let end = std::time::SystemTime::now()
42 .duration_since(std::time::UNIX_EPOCH)
43 .unwrap()
44 .as_millis() as u64;
45
46 end - start
47}
48
49#[cfg(target_arch = "wasm32")]
50fn get_wasm_time() -> u64 {
51 use web_sys::{window, Performance};
52 let window = window().expect("should have a window in this context");
53 let performance = window
54 .performance()
55 .expect("performance should be available");
56 performance.now() as u64
57}