Using library with std::time::Instant from WASM

I wrote a library for solving combinatorial optimization problems and I want to use it in webassembly in the browser.
It contains a struct with std::time::Instant:
pub struct SearchLimits { started: Instant, timeout: Instant,... }
As soon as Instant::now() is called in wasm it fails.
What are my options here?

  • Can i make Instant::now work somehow?
  • Can change the library in some minimally invasive way?

I don't actually care about the value of Instant when calling from wasm so I imagine i can use #[cfg] to replace the calls to now with something else but what? This is probably quite hacky which i don't mind for now but it would also be interessting what the proper way to deal with this is.

Can you change the struct to SearchLimits { started: Option<Instant>, timeout: Option<Instant>, …}? You'd be able to set them to None, which you can create without having access to timers.

Alternatively, consider using the instant crate. It re-exports Instant on non-WASM platforms, but swaps it out for a WASM-compatible reimplementation on WASM targets.


Thanks for your suggestions. For now i was able to cfg the instances away completely. I will checkout the instant crate it looks exactly like what i was looking for.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.