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