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
orwasm32-unknown-asmjs
and you enabled either thestdweb
or thewasm-bindgen
feature. This emulation is based on the javascriptperformance.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:
[]
= "0.1"
main.rs:
Using instant
for a WASM platform.
This example shows the use of the stdweb
feature. It would be similar with wasm-bindgen
.
Cargo.toml:
[]
= { = "0.1", = [ "stdweb" ] }
main.rs:
Using instant
for any platform enabling a feature transitively.
Cargo.toml:
[]
= [ "instant/stdweb" ]
= [ "instant/wasm-bindgen" ]
[]
= "0.1"
lib.rs:
Using the feature now
.
Cargo.toml:
[]
= [ "instant/stdweb" ]
= [ "instant/wasm-bindgen" ]
[]
= { = "0.1", = [ "now" ] }
lib.rs:
Using the feature now
without stdweb
or wasm-bindgen
.
Cargo.toml:
[]
= { = "0.", = [ "now" ] }
lib.rs:
javascript WASM bindings file: