diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/instant | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/instant')
-rw-r--r-- | third_party/rust/instant/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/instant/AUTHORS | 2 | ||||
-rw-r--r-- | third_party/rust/instant/Cargo.toml | 84 | ||||
-rw-r--r-- | third_party/rust/instant/LICENSE | 27 | ||||
-rw-r--r-- | third_party/rust/instant/README.md | 124 | ||||
-rw-r--r-- | third_party/rust/instant/src/lib.rs | 22 | ||||
-rw-r--r-- | third_party/rust/instant/src/native.rs | 7 | ||||
-rw-r--r-- | third_party/rust/instant/src/wasm.rs | 129 | ||||
-rw-r--r-- | third_party/rust/instant/tests/wasm.rs | 46 |
9 files changed, 442 insertions, 0 deletions
diff --git a/third_party/rust/instant/.cargo-checksum.json b/third_party/rust/instant/.cargo-checksum.json new file mode 100644 index 0000000000..2af1a09939 --- /dev/null +++ b/third_party/rust/instant/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"AUTHORS":"8bd4ba3271fa2f69d6897a0b14467b525adaecc4d7515698c801d07f32692c25","Cargo.toml":"cdb79ed66059b7a042327c1f3c439932821e93e37ab2a312c39b3d0a5abe51b7","LICENSE":"cafd574024390a2f68d29f16725abee480a0b8eba39ecd8dc78014182451894f","README.md":"8302eaaada69941829b5cf459e6b0ff9436e001a670b7f1521ae994c3920f4ca","src/lib.rs":"b74e05f72b3e5bd6ac0836e837991f83566e28d42602d9aa5fe7e35f5f60fc09","src/native.rs":"5c27a81932328b6c3ba4cbb86b754502fa91a38d1592e43e0219bc0c5a2fde8f","src/wasm.rs":"b83e783622406a1117c0fc058430bbc6dd07aa9272741fb21d35e2bb16e6e152","tests/wasm.rs":"2f30a8260fe266c8c40bd14397132763d016871c57cc708b433934c1c6300447"},"package":"cb1fc4429a33e1f80d41dc9fea4d108a88bec1de8053878898ae448a0b52f613"}
\ No newline at end of file diff --git a/third_party/rust/instant/AUTHORS b/third_party/rust/instant/AUTHORS new file mode 100644 index 0000000000..6f2ad5ba4a --- /dev/null +++ b/third_party/rust/instant/AUTHORS @@ -0,0 +1,2 @@ +Main developer: + * Sébastien Crozet <developer@crozet.re>
\ No newline at end of file diff --git a/third_party/rust/instant/Cargo.toml b/third_party/rust/instant/Cargo.toml new file mode 100644 index 0000000000..d11b2d3552 --- /dev/null +++ b/third_party/rust/instant/Cargo.toml @@ -0,0 +1,84 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +edition = "2018" +name = "instant" +version = "0.1.8" +authors = ["sebcrozet <developer@crozet.re>"] +description = "A partial replacement for std::time::Instant that works on WASM too." +readme = "README.md" +keywords = ["time", "wasm"] +license = "BSD-3-Clause" +repository = "https://github.com/sebcrozet/instant" +[dependencies.cfg-if] +version = "1.0" +[dev-dependencies.wasm-bindgen-test] +version = "0.3" + +[features] +now = ["time"] +wasm-bindgen = ["js-sys", "wasm-bindgen_rs", "web-sys"] +[target.asmjs-unknown-emscripten.dependencies.js-sys] +version = "0.3" +optional = true + +[target.asmjs-unknown-emscripten.dependencies.stdweb] +version = "0.4" +optional = true + +[target.asmjs-unknown-emscripten.dependencies.wasm-bindgen_rs] +version = "0.2" +optional = true +package = "wasm-bindgen" + +[target.asmjs-unknown-emscripten.dependencies.web-sys] +version = "0.3" +features = ["Window", "Performance", "PerformanceTiming"] +optional = true +[target."cfg(not(any(feature = \"stdweb\", feature = \"wasm-bindgen\")))".dependencies.time] +version = "0.2" +optional = true +[target.wasm32-unknown-emscripten.dependencies.js-sys] +version = "0.3" +optional = true + +[target.wasm32-unknown-emscripten.dependencies.stdweb] +version = "0.4" +optional = true + +[target.wasm32-unknown-emscripten.dependencies.wasm-bindgen_rs] +version = "0.2" +optional = true +package = "wasm-bindgen" + +[target.wasm32-unknown-emscripten.dependencies.web-sys] +version = "0.3" +features = ["Window", "Performance", "PerformanceTiming"] +optional = true +[target.wasm32-unknown-unknown.dependencies.js-sys] +version = "0.3" +optional = true + +[target.wasm32-unknown-unknown.dependencies.stdweb] +version = "0.4" +optional = true + +[target.wasm32-unknown-unknown.dependencies.wasm-bindgen_rs] +version = "0.2" +optional = true +package = "wasm-bindgen" + +[target.wasm32-unknown-unknown.dependencies.web-sys] +version = "0.3" +features = ["Window", "Performance", "PerformanceTiming"] +optional = true diff --git a/third_party/rust/instant/LICENSE b/third_party/rust/instant/LICENSE new file mode 100644 index 0000000000..a301457c08 --- /dev/null +++ b/third_party/rust/instant/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2019, Sébastien Crozet +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/third_party/rust/instant/README.md b/third_party/rust/instant/README.md new file mode 100644 index 0000000000..28f6bdd973 --- /dev/null +++ b/third_party/rust/instant/README.md @@ -0,0 +1,124 @@ +# Instant + +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: + +* A struct emulating the behavior of **std::time::Instant** if you are targeting `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. +* A type alias for `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. + +### The feature `now`. +By enabling the feature `now` the function `instant::now()` will be exported and will either: + +* Call `performance.now()` when compiling for a WASM platform with the features **stdweb** or **wasm-bindgen** enabled, or using a custom javascript function. +* Call `time::precise_time_s() * 1000.0` otherwise. + +The result is expressed in milliseconds. + +## Examples +### Using `instant` for a native platform. +_Cargo.toml_: +```toml +[dependencies] +instant = "0.1" +``` + +_main.rs_: +```rust +fn main() { + // Will be the same as `std::time::Instant`. + let now = instant::Instant::new(); +} +``` + +----- + +### Using `instant` for a WASM platform. +This example shows the use of the `stdweb` feature. It would be similar with `wasm-bindgen`. + +_Cargo.toml_: +```toml +[dependencies] +instant = { version = "0.1", features = [ "stdweb" ] } +``` + +_main.rs_: +```rust +fn main() { + // Will emulate `std::time::Instant` based on `performance.now()`. + let now = instant::Instant::new(); +} +``` + +----- + +### Using `instant` for any platform enabling a feature transitively. +_Cargo.toml_: +```toml +[features] +stdweb = [ "instant/stdweb" ] +wasm-bindgen = [ "instant/wasm-bindgen" ] + +[dependencies] +instant = "0.1" +``` + +_lib.rs_: +```rust +fn my_function() { + // Will select the proper implementation depending on the + // feature selected by the user. + let now = instant::Instant::new(); +} +``` + +----- + +### Using the feature `now`. +_Cargo.toml_: +```toml +[features] +stdweb = [ "instant/stdweb" ] +wasm-bindgen = [ "instant/wasm-bindgen" ] + +[dependencies] +instant = { version = "0.1", features = [ "now" ] } +``` + +_lib.rs_: +```rust +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. +} +``` + +### Using the feature `now` without `stdweb` or `wasm-bindgen`. +_Cargo.toml_: +```toml +[dependencies] +instant = { version = "0.", features = [ "now" ] } +``` + +_lib.rs_: +```rust +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_: +```js +function now() { + return Date.now() / 1000.0; +} +``` diff --git a/third_party/rust/instant/src/lib.rs b/third_party/rust/instant/src/lib.rs new file mode 100644 index 0000000000..077c9d4028 --- /dev/null +++ b/third_party/rust/instant/src/lib.rs @@ -0,0 +1,22 @@ +cfg_if::cfg_if! { + if #[cfg(any( + all(target_arch = "wasm32", not(target_os = "wasi")), + target_arch = "asmjs" + ))] { + #[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))] + #[macro_use] + extern crate stdweb; + + mod wasm; + pub use wasm::Instant; + #[cfg(feature = "now")] + pub use crate::wasm::now; + } else { + mod native; + pub use native::Instant; + #[cfg(feature = "now")] + pub use native::now; + } +} + +pub use std::time::Duration; diff --git a/third_party/rust/instant/src/native.rs b/third_party/rust/instant/src/native.rs new file mode 100644 index 0000000000..37d5da487d --- /dev/null +++ b/third_party/rust/instant/src/native.rs @@ -0,0 +1,7 @@ +pub type Instant = std::time::Instant; + +/// The current time, in milliseconds. +#[cfg(feature = "now")] +pub fn now() -> f64 { + time::precise_time_s() * 1000.0 +} diff --git a/third_party/rust/instant/src/wasm.rs b/third_party/rust/instant/src/wasm.rs new file mode 100644 index 0000000000..829ef3bd72 --- /dev/null +++ b/third_party/rust/instant/src/wasm.rs @@ -0,0 +1,129 @@ +use std::ops::{Add, AddAssign, Sub, SubAssign}; +use std::time::Duration; + +#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Hash)] +pub struct Instant(Duration); + +impl Ord for Instant { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.partial_cmp(other) + .expect("an instant should never be NaN or Inf.") + } +} +impl Eq for Instant {} + +impl Instant { + #[inline] + pub fn now() -> Self { + Instant(duration_from_f64(now())) + } + + #[inline] + pub fn duration_since(&self, earlier: Instant) -> Duration { + assert!( + earlier.0 <= self.0, + "`earlier` cannot be later than `self`." + ); + self.0 - earlier.0 + } + + #[inline] + pub fn elapsed(&self) -> Duration { + Self::now().duration_since(*self) + } + + /// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as + /// `Instant` (which means it's inside the bounds of the underlying data structure), `None` + /// otherwise. + #[inline] + pub fn checked_add(&self, duration: Duration) -> Option<Instant> { + self.0.checked_add(duration).map(Instant) + } + + /// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as + /// `Instant` (which means it's inside the bounds of the underlying data structure), `None` + /// otherwise. + #[inline] + pub fn checked_sub(&self, duration: Duration) -> Option<Instant> { + self.0.checked_sub(duration).map(Instant) + } +} + +impl Add<Duration> for Instant { + type Output = Self; + + #[inline] + fn add(self, rhs: Duration) -> Self { + Instant(self.0 + rhs) + } +} + +impl AddAssign<Duration> for Instant { + #[inline] + fn add_assign(&mut self, rhs: Duration) { + self.0 += rhs + } +} + +impl Sub<Duration> for Instant { + type Output = Self; + + #[inline] + fn sub(self, rhs: Duration) -> Self { + Instant(self.0 - rhs) + } +} + +impl Sub<Instant> for Instant { + type Output = Duration; + + #[inline] + fn sub(self, rhs: Instant) -> Duration { + self.duration_since(rhs) + } +} + +impl SubAssign<Duration> for Instant { + #[inline] + fn sub_assign(&mut self, rhs: Duration) { + self.0 -= rhs + } +} + +fn duration_from_f64(millis: f64) -> Duration { + Duration::from_millis(millis.trunc() as u64) + + Duration::from_nanos((millis.fract() * 1.0e6) as u64) +} + +#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))] +#[allow(unused_results)] // Needed because the js macro triggers it. +pub fn now() -> f64 { + use stdweb::unstable::TryInto; + + // https://developer.mozilla.org/en-US/docs/Web/API/Performance/now + let v = js! { return performance.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() +} + +// The JS now function is in a module so it won't have to be renamed +#[cfg(not(any(feature = "wasm-bindgen", feature = "stdweb")))] +mod js { + extern "C" { + pub fn now() -> f64; + } +} +// Make the unsafe extern function "safe" so it can be called like the other 'now' functions +#[cfg(not(any(feature = "wasm-bindgen", feature = "stdweb")))] +pub fn now() -> f64 { + unsafe { js::now() } +} diff --git a/third_party/rust/instant/tests/wasm.rs b/third_party/rust/instant/tests/wasm.rs new file mode 100644 index 0000000000..b1577adfa8 --- /dev/null +++ b/third_party/rust/instant/tests/wasm.rs @@ -0,0 +1,46 @@ +extern crate wasm_bindgen_test; + +use instant::Instant; +use std::time::Duration; +use wasm_bindgen_test::*; + +wasm_bindgen_test_configure!(run_in_browser); +// run these tests using: wasm-pack test --chrome --headless -- --features wasm-bindgen + +#[wasm_bindgen_test] +fn test_instant_now() { + let now = Instant::now(); + assert!(now.elapsed().as_nanos() > 0); +} + +#[wasm_bindgen_test] +fn test_duration() { + let now = Instant::now(); + let one_sec = Duration::from_secs(1); + assert!(now.elapsed() < one_sec); +} + +// Duration::new will overflow when you have u64::MAX seconds and one billion nanoseconds. +// <https://doc.rust-lang.org/std/time/struct.Duration.html#method.new> +const ONE_BILLION: u32 = 1_000_000_000; + +#[wasm_bindgen_test] +fn test_checked_add() { + let now = Instant::now(); + + assert!(now.checked_add(Duration::from_millis(1)).is_some()); + assert_eq!( + None, + now.checked_add(Duration::new(u64::MAX, ONE_BILLION - 1)) + ); +} + +#[wasm_bindgen_test] +fn test_checked_sub() { + let now = Instant::now(); + + assert!(now.checked_sub(Duration::from_millis(1)).is_some()); + assert!(now + .checked_sub(Duration::new(u64::MAX, ONE_BILLION - 1)) + .is_none()); +} |