From 503a98d7a899769edcb8a0bad4020bbd07c415cd Mon Sep 17 00:00:00 2001 From: iceiix <43691553+iceiix@users.noreply.github.com> Date: Sun, 27 Dec 2020 15:06:03 -0800 Subject: [PATCH] Use instant over std::time, for wasm compat (#449) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace std::time with the `instant` crate, which bridges to std::time on native but on wasm calls performance.now() instead of panicking. A step towards 🕸️ Web support #446 * logo: replace SystemTime/UNIX_EPOCH with Instant --- Cargo.lock | 2 ++ Cargo.toml | 1 + protocol/Cargo.lock | 10 ++++++++++ protocol/Cargo.toml | 1 + protocol/src/protocol/mod.rs | 2 +- src/entity/player.rs | 2 +- src/main.rs | 2 +- src/screen/server_list.rs | 2 +- src/ui/logo.rs | 6 ++++-- src/world/mod.rs | 2 +- 10 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 32486f7..d04f96b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2277,6 +2277,7 @@ dependencies = [ "cfb8", "flate2", "hex", + "instant", "log", "num-traits", "reqwest", @@ -2310,6 +2311,7 @@ dependencies = [ "glow", "glutin", "image", + "instant", "lazy_static", "log", "rand 0.7.3", diff --git a/Cargo.toml b/Cargo.toml index 41b64d7..b514a5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ collision = "0.20.1" rsa_public_encrypt_pkcs1 = "0.2.0" structopt = "0.3.21" clipboard = "0.5.0" +instant = "0.1.9" # clippy = "*" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/protocol/Cargo.lock b/protocol/Cargo.lock index 27a3dd6..761b321 100644 --- a/protocol/Cargo.lock +++ b/protocol/Cargo.lock @@ -432,6 +432,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "instant" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "iovec" version = "0.1.4" @@ -960,6 +969,7 @@ dependencies = [ "cfb8", "flate2", "hex", + "instant", "log", "num-traits", "reqwest", diff --git a/protocol/Cargo.toml b/protocol/Cargo.toml index 9a36706..70a8599 100644 --- a/protocol/Cargo.toml +++ b/protocol/Cargo.toml @@ -15,6 +15,7 @@ byteorder = "1.3.4" log = { version = "0.4.11", features = ["std"] } flate2 = { version = "1.0.19", features = ["rust_backend"], default-features = false } num-traits = "0.2.12" +instant = "0.1.9" [dependencies.steven_shared] path = "../shared" diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index 8778af0..e49ec59 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -29,6 +29,7 @@ use crate::shared::Position; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use flate2::read::{ZlibDecoder, ZlibEncoder}; use flate2::Compression; +use instant::{Duration, Instant}; use log::debug; use std::convert; use std::default; @@ -37,7 +38,6 @@ use std::io; use std::io::{Read, Write}; use std::net::TcpStream; use std::sync::atomic::{AtomicBool, AtomicI32, Ordering}; -use std::time::{Duration, Instant}; pub const SUPPORTED_PROTOCOLS: [i32; 24] = [ 754, 753, 751, 736, 735, 578, 575, 498, 490, 485, 480, 477, 452, 451, 404, 340, 316, 315, 210, diff --git a/src/entity/player.rs b/src/entity/player.rs index 0a2fa40..a0b6da8 100644 --- a/src/entity/player.rs +++ b/src/entity/player.rs @@ -12,9 +12,9 @@ use crate::types::Gamemode; use crate::world; use cgmath::{self, Decomposed, Matrix4, Point3, Quaternion, Rad, Rotation3, Vector3}; use collision::{Aabb, Aabb3}; +use instant::Instant; use std::collections::HashMap; use std::hash::BuildHasherDefault; -use std::time::Instant; pub fn add_systems(m: &mut ecs::Manager) { let sys = MovementHandler::new(m); diff --git a/src/main.rs b/src/main.rs index 2630319..0788855 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,9 +17,9 @@ #![allow(clippy::many_single_char_names)] // short variable names provide concise clarity #![allow(clippy::float_cmp)] // float comparison used to check if changed +use instant::{Duration, Instant}; use log::{error, info, warn}; use std::fs; -use std::time::{Duration, Instant}; extern crate steven_shared as shared; use structopt::StructOpt; diff --git a/src/screen/server_list.rs b/src/screen/server_list.rs index 073dd4f..2951d26 100644 --- a/src/screen/server_list.rs +++ b/src/screen/server_list.rs @@ -24,8 +24,8 @@ use crate::protocol; use crate::render; use crate::ui; +use instant::Duration; use rand::Rng; -use std::time::Duration; pub struct ServerList { elements: Option, diff --git a/src/ui/logo.rs b/src/ui/logo.rs index 697b23e..b33f1ec 100644 --- a/src/ui/logo.rs +++ b/src/ui/logo.rs @@ -1,10 +1,10 @@ use crate::render; use crate::resources; use crate::ui; +use instant::Instant; use rand::{self, seq::SliceRandom}; use std::f64::consts; use std::sync::{Arc, RwLock}; -use std::time::{SystemTime, UNIX_EPOCH}; pub struct Logo { _shadow: ui::BatchRef, @@ -15,6 +15,7 @@ pub struct Logo { text_orig_x: f64, text_index: isize, text_strings: Vec, + started: Instant, } impl Logo { @@ -131,11 +132,12 @@ impl Logo { text_orig_x, text_index: -1, text_strings, + started: Instant::now(), } } pub fn tick(&mut self, renderer: &mut render::Renderer) { - let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); + let now = Instant::now().duration_since(self.started); // Splash text let text_index = (now.as_secs() / 15) as isize % self.text_strings.len() as isize; diff --git a/src/world/mod.rs b/src/world/mod.rs index 9c9a6c4..bb3cdbc 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -206,7 +206,7 @@ impl World { #[allow(clippy::verbose_bit_mask)] // "llvm generates better code" for updates_performed & 0xFFF "on x86" pub fn tick(&mut self, m: &mut ecs::Manager) { - use std::time::Instant; + use instant::Instant; let start = Instant::now(); let mut updates_performed = 0; while !self.light_updates.is_empty() {