commit | c368efd594425abb1b71656c323abb5f3e693c53 | [log] [tgz] |
---|---|---|
author | Xin Li <delphij@google.com> | Tue Mar 08 11:03:43 2022 +0000 |
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | Tue Mar 08 11:03:43 2022 +0000 |
tree | 4a79f8bd26d95e83102eaa2bd02e03a49b61a4cb | |
parent | b753132403ede7933e83400971c712807b75068b [diff] | |
parent | dd4b72848ce0aa5da40d9e6db324ebabe29bc352 [diff] |
[automerger skipped] Merge Android 12L am: f9e865f869 -s ours am: dd4b72848c -s ours am skip reason: Merged-In Ibebaa77e1e957767a28e9045a47d254200994a84 with SHA-1 04b89fc2ea is already in history Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/instant/+/2013295 Change-Id: Ieed68ff76e808bbafd5f43f388bbd514e6c0cc64
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.
This crate also exports the function instant::now()
which returns a representation of the current time as an f64
, expressed in milliseconds, in a platform-agnostic way. instant::now()
will either:
performance.now()
when compiling for a WASM platform with the features stdweb or wasm-bindgen enabled, or using a custom javascript function.Note: The old feature, now
, has been deprecated. instant::now()
is always exported and the now
feature flag no longer has any effect. It remains listed in Cargo.toml
to avoid introducing breaking changes and may be removed in future versions.
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::now(); }
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::now(); }
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::now(); }
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::now(); }
instant::now()
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::Instant::now(); let now_milliseconds = instant::now(); // In milliseconds. }
now
without stdweb
or wasm-bindgen
.Cargo.toml:
[dependencies] instant = "0.1"
lib.rs:
fn my_function() { // Will use the 'now' javascript implementation. let now_instant = instant::Instant::now(); let now_milliseconds = instant::now(); // In milliseconds. }
javascript WASM bindings file:
function now() { return Date.now() / 1000.0; }