Skip to content

Add From<Duration> conversion for Instant#47

Open
itpetey wants to merge 2 commits into
daxpedda:mainfrom
itpetey:main
Open

Add From<Duration> conversion for Instant#47
itpetey wants to merge 2 commits into
daxpedda:mainfrom
itpetey:main

Conversation

@itpetey

@itpetey itpetey commented Jun 12, 2026

Copy link
Copy Markdown

This is a pretty innocuous change, but is a necessary escape hatch for using web_time on server-side WASM runtimes (e.g. wasmtime).

For example:

let d = Duration::from_nanos(get_time_from_host_fn());
Instant::from(d)

@itpetey

itpetey commented Jul 2, 2026

Copy link
Copy Markdown
Author

@daxpedda Any chance we could get this merged? It's a blocker for anyone using server-side WASM runtimes (e.g. wasmtime, wasmer etc.).

@daxpedda

daxpedda commented Jul 7, 2026

Copy link
Copy Markdown
Owner

I would be great if you could outline the use-case in the OP. I'm also not really sure what it means, you can't simply convert a Duration to Instant and expect it to work, e.g. Instant does add Performance.timeOrigin when compiled to atomics.

The bigger issue here is that this conversion doesn't exist in Std. This library specifically aims to be a drop-in replacement for std::time. This conversion would break cross-platform.

If you are just trying to create an Instant from an externally obtained DOMHighResTimeStamp, that would definitely be in scope, but it has to be added to web_time::web, which is the equivalent of std::arch::web.

@itpetey

itpetey commented Jul 8, 2026

Copy link
Copy Markdown
Author

Thanks for your feedback. First the use case - WebAssembly is a technology independent of the browser. Thus it is not guaranteed that on execution, WASM modules will have access to the JS API. For any WebAssembly implementation using wasmtime or another server-side-WASM runtime, web-time::Instant::now() breaks because Performance.timeOrigin doesn't exist. But this is a runtime breakage. We can shim around that by not calling the now constructor at all, hence Instant::from(Duration).

A real world example - quinn. quinn uses web-time as its "WASM solution", not accounting for the fact that web-time only solves for web and not server-side WASM. Introducing downstream patches is arguably the more correct solution if you wanted to keep this lib as web-only, but given the number of crates relying on web-time already, I hoped we could make web-time a proper all-of-WASM solution. Granted, my patch is somewhat opaque, but works for quinn without being obtrusive. Here's a sample of code that compiles and runs on wasmtime, given only the patch I PR'd:

impl quinn::Runtime for SeliumQuinnRuntime {
    fn now(&self) -> web_time::Instant {
        let i = crate::time::Instant::now().expect("failed to get monotonic time from host");
        let d = std::time::Duration::from_nanos(i.as_nanos());
        web_time::Instant::from(d)
    }

    ...
}

Selium has its own client library (the crate in the above code) and host ABI. No JS involved. I use web_time::Instant::from(Duration) to shim around the lack of ABI support; and it works great!

I take your point about lack of compatibility with the std lib however. I've updated my patch to extend web.rs as you suggested. Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants