Skip to content
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

std::time replacement for wasm #449

Merged
merged 12 commits into from
Dec 27, 2020
Merged

std::time replacement for wasm #449

merged 12 commits into from
Dec 27, 2020

Conversation

iceiix
Copy link
Owner

@iceiix iceiix commented Dec 27, 2020

The wasm32-unknown-unknown target does not support std::time, will need to find a replacement for 🕸️ Web support #446

@iceiix iceiix mentioned this pull request Dec 27, 2020
29 tasks
@iceiix
Copy link
Owner Author

iceiix commented Dec 27, 2020

Uses of std::time:

  • src/main.rs - Duration, Instant - FPS cap (optional)
  • src/entity/player.rs - Instant - when_last_jump_pressed/released
  • src/screen/server_list.rs - Duration - ping
  • src/ui/logo.rs - SystemTime, UNIX_EPOCH - animation
  • src/world/mod.rs - Instant - lighting update ticks
  • protocol/src/protocol/mod.rs - Duration, Instant - server ping

https://doc.rust-lang.org/std/time/struct.Instant.html - opaque nondecreasing clock, only useful with Duration
https://doc.rust-lang.org/std/time/struct.Duration.html - seconds and nanoseconds (if not precise, rounded up)

The web platform has performance.now(), returning milliseconds increasing at a constant rate, not limited to ms-precision (floating-point allows up to µs precision, but Firefox at least rounds to 1 ms). Sounds exactly like what we want for Instant and Duration.

Found this crate, a drop-in replacement (would be nice if these crates could override std, alas...) for std::time::Instant working on WASM too: https://lib.rs/crates/instant https://github.com/sebcrozet/instant

There's also https://lib.rs/crates/wasm-timer for std::time::Instant and futures-timer (tokio-timer on native). https://lib.rs/search?q=wasm+std This looks promising too: https://lib.rs/crates/wasm_thread "An std thread replacement for wasm32 target"

Although there's an open pull request to the `instant` crate to support
SystemTime and UNIX_EPOCH (sebcrozet/instant#9),
it is simpler to change ui/logo to simply use Instant and Duration.
@iceiix iceiix changed the title std::time vs wasm std::time replacement for wasm Dec 27, 2020
@iceiix iceiix marked this pull request as ready for review December 27, 2020 23:03
@iceiix iceiix merged commit 503a98d into master Dec 27, 2020
@iceiix iceiix deleted the no_time branch December 27, 2020 23:06
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.

1 participant