#time #performance #replace #consider #native #alias

unmaintained instant

Unmaintained, consider using web-time instead - A partial replacement for std::time::Instant that works on WASM to

14 releases

0.1.13 May 17, 2024
0.1.12 Oct 18, 2021
0.1.11 Sep 23, 2021
0.1.10 Jul 11, 2021
0.1.1 Apr 9, 2019

#898 in WebAssembly

Download history 1115644/week @ 2024-08-15 1165263/week @ 2024-08-22 1073350/week @ 2024-08-29 1187709/week @ 2024-09-05 1140081/week @ 2024-09-12 1086601/week @ 2024-09-19 1160155/week @ 2024-09-26 1167222/week @ 2024-10-03 1140059/week @ 2024-10-10 1201903/week @ 2024-10-17 1178965/week @ 2024-10-24 1121548/week @ 2024-10-31 1122985/week @ 2024-11-07 1139047/week @ 2024-11-14 1077139/week @ 2024-11-21 976468/week @ 2024-11-28

4,527,502 downloads per month
Used in 8,088 crates (195 directly)

BSD-3-Clause

17KB
217 lines

Instant

This crate is no longer maintained. Please consider creating a fork or using web-time instead. Or reach out if you are interested in taking over its maintenance.

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.

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:

  • Call performance.now() when compiling for a WASM platform with the features stdweb or wasm-bindgen enabled, or using a custom javascript function.
  • Return the time elapsed since the Unix Epoch on native, non-WASM platforms.

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.

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

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

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

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

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

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

Dependencies

~0–2.7MB
~50K SLoC