commit | 1544970a9fa5419c082b617268c13d9bb48bf537 | [log] [tgz] |
---|---|---|
author | Jeff Vander Stoep <jeffv@google.com> | Thu Nov 05 16:59:25 2020 +0000 |
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | Thu Nov 05 16:59:25 2020 +0000 |
tree | 6fbd9f7b81797e029d1e2c5eca8bf4cf6f06619c | |
parent | 26041ccd7a20d18c463d52a3f6ccd723cef1ca70 [diff] | |
parent | 07ca6a2126c86ce00608a119b940558a407fc068 [diff] |
TEST_MAPPING: test dependers of this crate am: f74b866939 am: 07ca6a2126 Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/instant/+/1488750 Change-Id: Ic16f4123cc98848a67ca1e06d1dde3c34ee2cce8
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 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; }