diff options
author | Haibo Huang <hhb@google.com> | 2020-11-19 17:08:55 -0800 |
---|---|---|
committer | Jeff Vander Stoep <jeffv@google.com> | 2021-01-15 15:52:38 +0100 |
commit | ebac46102a63413e1bbbfea012e7f440a9dc8109 (patch) | |
tree | 41e7124f4626606cc15fd54021a363b804b92ce5 | |
parent | 07ca6a2126c86ce00608a119b940558a407fc068 (diff) | |
download | instant-ebac46102a63413e1bbbfea012e7f440a9dc8109.tar.gz |
Upgrade rust/crates/instant to 0.1.9platform-tools-31.0.0
Test: make
Change-Id: Ic84c8d65797c8dc71e17ab4f04e2dc6d4d52e889
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | Cargo.toml.orig | 3 | ||||
-rw-r--r-- | METADATA | 8 | ||||
-rw-r--r-- | README.md | 20 | ||||
-rw-r--r-- | TEST_MAPPING | 6 | ||||
-rw-r--r-- | src/wasm.rs | 21 | ||||
-rw-r--r-- | tests/wasm.rs | 3 |
8 files changed, 48 insertions, 18 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index cd27d63..0fa03ad 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "3819881b22c13652f3d625f6e7668ccfb2bd27bd" + "sha1": "eff71cffcc21f0b6d84a7dc3009cacb9ff16b4ea" } } @@ -13,7 +13,7 @@ [package] edition = "2018" name = "instant" -version = "0.1.8" +version = "0.1.9" authors = ["sebcrozet <developer@crozet.re>"] description = "A partial replacement for std::time::Instant that works on WASM too." readme = "README.md" @@ -26,6 +26,7 @@ version = "1.0" version = "0.3" [features] +inaccurate = [] now = ["time"] wasm-bindgen = ["js-sys", "wasm-bindgen_rs", "web-sys"] [target.asmjs-unknown-emscripten.dependencies.js-sys] diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 8a7047e..f6be57f 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "instant" -version = "0.1.8" +version = "0.1.9" authors = ["sebcrozet <developer@crozet.re>"] description = "A partial replacement for std::time::Instant that works on WASM too." repository = "https://github.com/sebcrozet/instant" @@ -12,6 +12,7 @@ edition = "2018" [features] now = [ "time" ] wasm-bindgen = ["js-sys", "wasm-bindgen_rs", "web-sys"] +inaccurate = [] [dependencies] cfg-if = "1.0" @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/instant/instant-0.1.8.crate" + value: "https://static.crates.io/crates/instant/instant-0.1.9.crate" } - version: "0.1.8" + version: "0.1.9" license_type: NOTICE last_upgrade_date { year: 2020 - month: 10 - day: 26 + month: 11 + day: 19 } } @@ -57,6 +57,26 @@ fn main() { ----- +### Using `instant` for a WASM platform where `performance.now()` is not available. +This example shows the use of the `inaccurate` feature. + +_Cargo.toml_: +```toml +[dependencies] +instant = { version = "0.1", features = [ "wasm-bindgen", "inaccurate" ] } +``` + +_main.rs_: +```rust +fn main() { + // Will emulate `std::time::Instant` based on `Date.now()`. + let now = instant::Instant::new(); +} +``` + + +----- + ### Using `instant` for any platform enabling a feature transitively. _Cargo.toml_: ```toml diff --git a/TEST_MAPPING b/TEST_MAPPING index 5058d5e..7213fe9 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -1,11 +1,7 @@ -// Generated by cargo2android.py for tests in Android.bp +// Generated by cargo2android.py for tests that depend on this crate. { "presubmit": [ { - "host": true, - "name": "parking_lot_core_host_test_src_lib" - }, - { "name": "parking_lot_core_device_test_src_lib" } ] diff --git a/src/wasm.rs b/src/wasm.rs index 829ef3b..986bbf6 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -101,18 +101,27 @@ pub fn now() -> f64 { use stdweb::unstable::TryInto; // https://developer.mozilla.org/en-US/docs/Web/API/Performance/now + #[cfg(not(feature = "inaccurate"))] let v = js! { return performance.now(); }; + #[cfg(feature = "inaccurate")] + let v = js! { return Date.now(); }; v.try_into().unwrap() } #[cfg(feature = "wasm-bindgen")] pub fn now() -> f64 { - use wasm_bindgen_rs::prelude::*; - use wasm_bindgen_rs::JsCast; - js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("performance")) - .expect("failed to get performance from global object") - .unchecked_into::<web_sys::Performance>() - .now() + #[cfg(not(feature = "inaccurate"))] + let now = { + use wasm_bindgen_rs::prelude::*; + use wasm_bindgen_rs::JsCast; + js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("performance")) + .expect("failed to get performance from global object") + .unchecked_into::<web_sys::Performance>() + .now() + }; + #[cfg(feature = "inaccurate")] + let now = js_sys::Date::now(); + now } // The JS now function is in a module so it won't have to be renamed diff --git a/tests/wasm.rs b/tests/wasm.rs index b1577ad..863eefd 100644 --- a/tests/wasm.rs +++ b/tests/wasm.rs @@ -10,6 +10,9 @@ wasm_bindgen_test_configure!(run_in_browser); #[wasm_bindgen_test] fn test_instant_now() { let now = Instant::now(); + #[cfg(feature = "inaccurate")] + while now.elapsed().as_millis() == 0 {} + #[cfg(not(feature = "inaccurate"))] assert!(now.elapsed().as_nanos() > 0); } |