3 If you call `std::time::Instant::now()` on a WASM platform, it will panic. This crate provides a partial
4 replacement for `std::time::Instant` that works on WASM too. This defines the type `instant::Instant` which is:
6 * A struct emulating the behavior of **std::time::Instant** if you are targeting `wasm32-unknown-unknown` or `wasm32-unknown-asmjs`
7 **and** you enabled either the `stdweb` or the `wasm-bindgen` feature. This emulation is based on the javascript `performance.now()` function.
8 * A type alias for `std::time::Instant` otherwise.
12 Note that even if the **stdweb** or **wasm-bindgen** feature is enabled, this crate will continue to rely on `std::time::Instant`
13 as long as you are not targeting wasm32. This allows for portable code that will work on both native and WASM platforms.
15 ### The feature `now`.
16 By enabling the feature `now` the function `instant::now()` will be exported and will either:
18 * Call `performance.now()` when compiling for a WASM platform with the features **stdweb** or **wasm-bindgen** enabled, or using a custom javascript function.
19 * Call `time::precise_time_s() * 1000.0` otherwise.
21 The result is expressed in milliseconds.
24 ### Using `instant` for a native platform.
34 // Will be the same as `std::time::Instant`.
35 let now = instant::Instant::new();
41 ### Using `instant` for a WASM platform.
42 This example shows the use of the `stdweb` feature. It would be similar with `wasm-bindgen`.
47 instant = { version = "0.1", features = [ "stdweb" ] }
53 // Will emulate `std::time::Instant` based on `performance.now()`.
54 let now = instant::Instant::new();
60 ### Using `instant` for a WASM platform where `performance.now()` is not available.
61 This example shows the use of the `inaccurate` feature.
66 instant = { version = "0.1", features = [ "wasm-bindgen", "inaccurate" ] }
72 // Will emulate `std::time::Instant` based on `Date.now()`.
73 let now = instant::Instant::new();
80 ### Using `instant` for any platform enabling a feature transitively.
84 stdweb = [ "instant/stdweb" ]
85 wasm-bindgen = [ "instant/wasm-bindgen" ]
94 // Will select the proper implementation depending on the
95 // feature selected by the user.
96 let now = instant::Instant::new();
102 ### Using the feature `now`.
106 stdweb = [ "instant/stdweb" ]
107 wasm-bindgen = [ "instant/wasm-bindgen" ]
110 instant = { version = "0.1", features = [ "now" ] }
116 // Will select the proper implementation depending on the
117 // feature selected by the user.
118 let now_instant = instant::Instant::new();
119 let now_milliseconds = instant::now(); // In milliseconds.
123 ### Using the feature `now` without `stdweb` or `wasm-bindgen`.
127 instant = { version = "0.", features = [ "now" ] }
133 // Will use the 'now' javascript implementation.
134 let now_instant = instant::Instant::new();
135 let now_milliseconds = instant::now(); // In milliseconds.
139 _javascript WASM bindings file_:
142 return Date.now() / 1000.0;