#time #wasm

instant

A partial replacement for std::time::Instant that works on WASM too

5 releases

✓ Uses Rust 2018 edition

0.1.4 May 18, 2020
0.1.3 Apr 24, 2020
0.1.2 Oct 1, 2019
0.1.1 Apr 9, 2019
0.1.0 Apr 9, 2019

#8 in WebAssembly

Download history 6773/week @ 2020-02-09 6831/week @ 2020-02-16 5958/week @ 2020-02-23 5653/week @ 2020-03-01 5108/week @ 2020-03-08 5860/week @ 2020-03-15 6616/week @ 2020-03-22 7004/week @ 2020-03-29 7502/week @ 2020-04-05 8003/week @ 2020-04-12 9139/week @ 2020-04-19 12249/week @ 2020-04-26 12452/week @ 2020-05-03 14801/week @ 2020-05-10 16681/week @ 2020-05-17 15765/week @ 2020-05-24

36,080 downloads per month
Used in 206 crates (23 directly)

BSD-3-Clause

11KB
126 lines

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:

[dependencies]
instant = "0.1"

main.rs:

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:

[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();
}

Using 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();
}

Using the feature 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.
}

Using the feature 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;
}

Dependencies

~0–2.4MB
~46K SLoC