-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use web-time for tokio::time::Instant on wasm32-unknown-unknown #6740
Use web-time for tokio::time::Instant on wasm32-unknown-unknown #6740
Conversation
85bd0db
to
1359f29
Compare
I was under the impression that |
I appreciate it actually, so I can help out if necessary!
Both Emscripten and WASI support
This all depends of course on the environment. E.g. I'm not particularly familiar with Emscripten and WASI, but I'm happy to answer any questions around |
The current situation is that Tokio isn't really usable in browser wasm because it will block the UI thread when it goes to sleep. However, I believe it should work from web workers as blocking the worker is okay. |
f8393e2
to
a706a57
Compare
@@ -96,6 +96,7 @@ mod instant; | |||
pub use self::instant::Instant; | |||
|
|||
mod interval; | |||
pub(crate) use instant::InstantInner; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love this solution but think it might be the least bad? I played with a few different refactors to deal with the Inner
of Clock
in the cfg_test_util
feature block using std::time::Instant
. I'm presume is for testing timeouts and such and really didn't want to touch it. I think using the same feature-flagged type alias might be the most maintainable.
Thoughts?
a706a57
to
085ed8e
Compare
What cases does this actually improve the situation in? If it only makes a difference in non-web-worker browser-wasm, then I don't think it makes sense. |
Ping on this? |
I made an app using egui, and I am currently using this via the cargo patch section. My web-app is only a "port" I made of my app. I am unable to demonstrate the app currently because of some web CORS issues (so unrelated to this pr or crate). |
Yeah. After authoring this PR and using it more I ran into other issues and wish for more features from the
Yeah, my use case is similar to @C0D3-M4513R's. I have an existing project which I'm trying to support running in the web browser. The existing codebase uses a lot of I agree that it's pretty un-intuitive to be able to do I definitely tried to keep this PR small and straight forward. Would it be of interested to expand the scope to include |
Unfortunately, I think non-web-worker browser wasm is out of scope for Tokio right now. |
Motivation
tokio::time::Instant
and such dependencies compile for wasm32-unknown-unknown however whenstd::time::Instant::now()
is called, it panics. This more or less renders,tokio::time::*
useless as they rely ontokio::time::Instant::now()
(which then panics in the stdlib):tokio::time::timeout
tokio::time::sleep
tokio::time::Instant
Solution
I've done my best to try and research the various PRs. This is related to #6178 and tangentially related to #4827.
The
web-time
crate is referenced in a few issues and is a "Complete drop-in replacement for std::time that works in browsers."Tokio is a pretty critical crate in the rust ecosystem and I don't take adding a new dependency lightly. When looking the other top level tokio dependencies, there are just two crates of even remotely similar popularity (
pin-project-lite
andsignal-hook
) so this dependency is definitely on the lesser-popularity side. The author/maintainer (daxpedda
) is a pretty common contributor to winit (where I first recognized the handle) and a bunch of wasm related projects so I think they're pretty responsible (we've not met, we just contribute to the same repos from time to time.)This is a sibling PR to tokio-rs/tracing#2758.
cc: @daxpedda (hope you don't mind the tag)
There's still the issue that
std::thread::sleep
is unsupported for wasm which is the next problem. I figure it's good to see the interest level of these changes before getting too far into the weeds.