| commit | 59506c3bb214deda76392af3da52b5aa82e2e57d | [log] [tgz] |
|---|---|---|
| author | Xin Li <[email protected]> | Sat Feb 20 13:16:20 2021 +0000 |
| committer | Automerger Merge Worker <[email protected]> | Sat Feb 20 13:16:20 2021 +0000 |
| tree | cca5bb2f710f5667d61900a53bea9e4dacef32a3 | |
| parent | 582c3df5cfa631ec44fe402dac7a93d7111656d8 [diff] | |
| parent | a18edb3289aec2ac3b8adcc24ddf8c70b717a0bc [diff] |
[automerger skipped] Mark ab/7061308 as merged in stage. am: 04bd053b28 -s ours am: 1afc956599 -s ours am: a18edb3289 -s ours am skip reason: Change-Id Id5737bba7df51ee3c84c339917f59b00d9f283f6 with SHA-1 7b1b61840e is in history Original change: undetermined MUST ONLY BE SUBMITTED BY AUTOMERGER Change-Id: I40129744771a0115520830d9e5f5884ff688ace0
If you call std::time::Instant::now() on a WASM platform, it will panic. This crate provides a partial replacement for std::time::Instant that works on WASM too. This defines the type instant::Instant which is:
wasm32-unknown-unknown or wasm32-unknown-asmjs and you enabled either the stdweb or the wasm-bindgen feature. This emulation is based on the javascript performance.now() function.std::time::Instant otherwise.Note that even if the stdweb or wasm-bindgen feature is enabled, this crate will continue to rely on std::time::Instant as long as you are not targeting wasm32. This allows for portable code that will work on both native and WASM platforms.
now.By enabling the feature now the function instant::now() will be exported and will either:
performance.now() when compiling for a WASM platform with the features stdweb or wasm-bindgen enabled, or using a custom javascript function.time::precise_time_s() * 1000.0 otherwise.The result is expressed in milliseconds.
instant for a native platform.Cargo.toml:
[dependencies] instant = "0.1"
main.rs:
fn main() { // Will be the same as `std::time::Instant`. let now = instant::Instant::new(); }
instant for a WASM platform.This example shows the use of the stdweb feature. It would be similar with wasm-bindgen.
Cargo.toml:
[dependencies] instant = { version = "0.1", features = [ "stdweb" ] }
main.rs:
fn main() { // Will emulate `std::time::Instant` based on `performance.now()`. let now = instant::Instant::new(); }
instant for a WASM platform where performance.now() is not available.This example shows the use of the inaccurate feature.
Cargo.toml:
[dependencies] instant = { version = "0.1", features = [ "wasm-bindgen", "inaccurate" ] }
main.rs:
fn main() { // Will emulate `std::time::Instant` based on `Date.now()`. let now = instant::Instant::new(); }
instant for any platform enabling a feature transitively.Cargo.toml:
[features] stdweb = [ "instant/stdweb" ] wasm-bindgen = [ "instant/wasm-bindgen" ] [dependencies] instant = "0.1"
lib.rs:
fn my_function() { // Will select the proper implementation depending on the // feature selected by the user. let now = instant::Instant::new(); }
now.Cargo.toml:
[features] stdweb = [ "instant/stdweb" ] wasm-bindgen = [ "instant/wasm-bindgen" ] [dependencies] instant = { version = "0.1", features = [ "now" ] }
lib.rs:
fn my_function() { // Will select the proper implementation depending on the // feature selected by the user. let now_instant = instant::Instant::new(); let now_milliseconds = instant::now(); // In milliseconds. }
now without stdweb or wasm-bindgen.Cargo.toml:
[dependencies] instant = { version = "0.", features = [ "now" ] }
lib.rs:
fn my_function() { // Will use the 'now' javascript implementation. let now_instant = instant::Instant::new(); let now_milliseconds = instant::now(); // In milliseconds. }
javascript WASM bindings file:
function now() { return Date.now() / 1000.0; }