use wasm-friendly instant::Instant everywhere (#895)

* use instant::Instant everywhere
* reexport instant::{Duration, Instant} from bevy_utils
This commit is contained in:
Mariusz Kryński 2020-11-22 01:38:24 +01:00 committed by GitHub
parent d458406540
commit d96493a42a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 30 additions and 50 deletions

View File

@ -27,4 +27,3 @@ serde = { version = "1.0", features = ["derive"] }
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" } wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3", features = [ "Window" ] } web-sys = { version = "0.3", features = [ "Window" ] }
instant = { version = "0.1", features = ["wasm-bindgen"] }

View File

@ -4,12 +4,7 @@ use crate::{
event::{EventReader, Events}, event::{EventReader, Events},
plugin::Plugin, plugin::Plugin,
}; };
use std::time::Duration; use bevy_utils::{Duration, Instant};
#[cfg(target_arch = "wasm32")]
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use std::{thread, time::Instant};
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
@ -104,7 +99,7 @@ impl Plugin for ScheduleRunnerPlugin {
{ {
while let Ok(delay) = tick(&mut app, wait) { while let Ok(delay) = tick(&mut app, wait) {
if let Some(delay) = delay { if let Some(delay) = delay {
thread::sleep(delay); std::thread::sleep(delay);
} }
} }
} }

View File

@ -22,6 +22,3 @@ bevy_type_registry = { path = "../bevy_type_registry", version = "0.3.0" }
bevy_math = { path = "../bevy_math", version = "0.3.0" } bevy_math = { path = "../bevy_math", version = "0.3.0" }
bevy_utils = { path = "../bevy_utils", version = "0.3.0" } bevy_utils = { path = "../bevy_utils", version = "0.3.0" }
bevy_tasks = { path = "../bevy_tasks", version = "0.3.0" } bevy_tasks = { path = "../bevy_tasks", version = "0.3.0" }
[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = { version = "0.1", features = ["wasm-bindgen"] }

View File

@ -1,10 +1,5 @@
use bevy_ecs::ResMut; use bevy_ecs::ResMut;
use std::time::Duration; use bevy_utils::{Duration, Instant};
#[cfg(target_arch = "wasm32")]
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
/// Tracks elapsed time since the last update and since the App has started /// Tracks elapsed time since the last update and since the App has started
#[derive(Debug)] #[derive(Debug)]

View File

@ -1,7 +1,7 @@
use crate::time::Time; use crate::time::Time;
use bevy_ecs::prelude::*; use bevy_ecs::prelude::*;
use bevy_property::Properties; use bevy_property::Properties;
use std::time::Duration; use bevy_utils::Duration;
/// Tracks elapsed time. Enters the finished state once `duration` is reached. /// Tracks elapsed time. Enters the finished state once `duration` is reached.
/// ///

View File

@ -23,6 +23,3 @@ bevy_utils = { path = "../bevy_utils", version = "0.3.0" }
# other # other
uuid = { version = "0.8", features = ["v4", "serde"] } uuid = { version = "0.8", features = ["v4", "serde"] }
parking_lot = "0.11.0" parking_lot = "0.11.0"
[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = { version = "0.1", features = ["wasm-bindgen"] }

View File

@ -1,8 +1,5 @@
use bevy_utils::HashMap; use bevy_utils::{Duration, HashMap, Instant};
use std::{ use std::collections::VecDeque;
collections::VecDeque,
time::{Duration, SystemTime},
};
use uuid::Uuid; use uuid::Uuid;
/// Unique identifier for a [Diagnostic] /// Unique identifier for a [Diagnostic]
@ -24,7 +21,7 @@ impl Default for DiagnosticId {
/// A single measurement of a [Diagnostic] /// A single measurement of a [Diagnostic]
#[derive(Debug)] #[derive(Debug)]
pub struct DiagnosticMeasurement { pub struct DiagnosticMeasurement {
pub time: SystemTime, pub time: Instant,
pub value: f64, pub value: f64,
} }
@ -41,7 +38,7 @@ pub struct Diagnostic {
impl Diagnostic { impl Diagnostic {
pub fn add_measurement(&mut self, value: f64) { pub fn add_measurement(&mut self, value: f64) {
let time = SystemTime::now(); let time = Instant::now();
if self.history.len() == self.max_history_length { if self.history.len() == self.max_history_length {
if let Some(removed_diagnostic) = self.history.pop_back() { if let Some(removed_diagnostic) = self.history.pop_back() {
self.sum -= removed_diagnostic.value; self.sum -= removed_diagnostic.value;
@ -90,7 +87,7 @@ impl Diagnostic {
if let Some(oldest) = self.history.back() { if let Some(oldest) = self.history.back() {
if let Some(newest) = self.history.front() { if let Some(newest) = self.history.front() {
return newest.time.duration_since(oldest.time).ok(); return Some(newest.time.duration_since(oldest.time));
} }
} }

View File

@ -2,7 +2,7 @@ use super::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_app::prelude::*; use bevy_app::prelude::*;
use bevy_core::{Time, Timer}; use bevy_core::{Time, Timer};
use bevy_ecs::{Res, ResMut}; use bevy_ecs::{Res, ResMut};
use std::time::Duration; use bevy_utils::Duration;
/// An App Plugin that prints diagnostics to the console /// An App Plugin that prints diagnostics to the console
pub struct PrintDiagnosticsPlugin { pub struct PrintDiagnosticsPlugin {

View File

@ -20,6 +20,7 @@ futures-lite = "1.4.0"
event-listener = "2.4.0" event-listener = "2.4.0"
async-executor = "1.3.0" async-executor = "1.3.0"
async-channel = "1.4.2" async-channel = "1.4.2"
instant = { version = "0.1", features = ["wasm-bindgen"] }
num_cpus = "1" num_cpus = "1"
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4" wasm-bindgen-futures = "0.4"

View File

@ -10,12 +10,12 @@ fn main() {
.num_threads(4) .num_threads(4)
.build(); .build();
let t0 = std::time::Instant::now(); let t0 = instant::Instant::now();
pool.scope(|s| { pool.scope(|s| {
for i in 0..40 { for i in 0..40 {
s.spawn(async move { s.spawn(async move {
let now = std::time::Instant::now(); let now = instant::Instant::now();
while std::time::Instant::now() - now < std::time::Duration::from_millis(100) { while instant::Instant::now() - now < instant::Duration::from_millis(100) {
// spin, simulating work being done // spin, simulating work being done
} }
@ -28,6 +28,6 @@ fn main() {
} }
}); });
let t1 = std::time::Instant::now(); let t1 = instant::Instant::now();
println!("all tasks finished in {} secs", (t1 - t0).as_secs_f32()); println!("all tasks finished in {} secs", (t1 - t0).as_secs_f32());
} }

View File

@ -13,8 +13,8 @@ fn main() {
for i in 0..1 { for i in 0..1 {
s.spawn(async move { s.spawn(async move {
println!("Blocking for 10 seconds"); println!("Blocking for 10 seconds");
let now = std::time::Instant::now(); let now = instant::Instant::now();
while std::time::Instant::now() - now < std::time::Duration::from_millis(10000) { while instant::Instant::now() - now < instant::Duration::from_millis(10000) {
// spin, simulating work being done // spin, simulating work being done
} }

View File

@ -99,7 +99,7 @@ pub fn countdown_event_ready() {
std::thread::spawn(move || futures_lite::future::block_on(countdown_event_clone.listen())); std::thread::spawn(move || futures_lite::future::block_on(countdown_event_clone.listen()));
// Pause to give the new thread time to start blocking (ugly hack) // Pause to give the new thread time to start blocking (ugly hack)
std::thread::sleep(std::time::Duration::from_millis(100)); std::thread::sleep(instant::Duration::from_millis(100));
countdown_event.decrement(); countdown_event.decrement();
handle.join().unwrap(); handle.join().unwrap();
@ -121,7 +121,7 @@ pub fn event_resets_if_listeners_are_cleared() {
// Verify that we are still blocked // Verify that we are still blocked
assert_eq!( assert_eq!(
false, false,
listener2.wait_timeout(std::time::Duration::from_millis(10)) listener2.wait_timeout(instant::Duration::from_millis(10))
); );
// Notify all and verify the remaining listener is notified // Notify all and verify the remaining listener is notified

View File

@ -15,6 +15,7 @@ keywords = ["bevy"]
[dependencies] [dependencies]
ahash = "0.5.3" ahash = "0.5.3"
tracing = {version = "0.1", features = ["release_max_level_info"]} tracing = {version = "0.1", features = ["release_max_level_info"]}
instant = { version = "0.1", features = ["wasm-bindgen"] }
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = {version = "0.2.0", features = ["js"]} getrandom = {version = "0.2.0", features = ["js"]}

View File

@ -1,7 +1,7 @@
use ahash::RandomState;
use std::{future::Future, pin::Pin};
pub use ahash::AHasher; pub use ahash::AHasher;
use ahash::RandomState;
pub use instant::{Duration, Instant};
use std::{future::Future, pin::Pin};
pub use tracing; pub use tracing;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]

View File

@ -1,5 +1,4 @@
use bevy::{app::ScheduleRunnerSettings, prelude::*}; use bevy::{app::ScheduleRunnerSettings, prelude::*, utils::Duration};
use std::time::Duration;
// This example only enables a minimal set of plugins required for bevy to run. // This example only enables a minimal set of plugins required for bevy to run.
// You can also completely remove rendering / windowing Plugin code from bevy // You can also completely remove rendering / windowing Plugin code from bevy

View File

@ -1,5 +1,4 @@
use bevy::prelude::*; use bevy::{prelude::*, utils::Duration};
use std::time::Duration;
/// Plugins are the foundation of Bevy. They are scoped sets of components, resources, and systems /// Plugins are the foundation of Bevy. They are scoped sets of components, resources, and systems
/// that provide a specific piece of functionality (generally the smaller the scope, the better). /// that provide a specific piece of functionality (generally the smaller the scope, the better).

View File

@ -1,9 +1,9 @@
use bevy::{ use bevy::{
app::{AppExit, ScheduleRunnerPlugin, ScheduleRunnerSettings}, app::{AppExit, ScheduleRunnerPlugin, ScheduleRunnerSettings},
prelude::*, prelude::*,
utils::Duration,
}; };
use rand::random; use rand::random;
use std::time::Duration;
/// This is a guided introduction to Bevy's "Entity Component System" (ECS) /// This is a guided introduction to Bevy's "Entity Component System" (ECS)
/// All Bevy app logic is built using the ECS pattern, so definitely pay attention! /// All Bevy app logic is built using the ECS pattern, so definitely pay attention!

View File

@ -1,4 +1,4 @@
use bevy::{prelude::*, type_registry::TypeRegistry}; use bevy::{prelude::*, type_registry::TypeRegistry, utils::Duration};
/// This example illustrates loading and saving scenes from files /// This example illustrates loading and saving scenes from files
fn main() { fn main() {
@ -37,7 +37,7 @@ struct ComponentA {
struct ComponentB { struct ComponentB {
pub value: String, pub value: String,
#[property(ignore)] #[property(ignore)]
pub time_since_startup: std::time::Duration, pub time_since_startup: Duration,
} }
impl FromResources for ComponentB { impl FromResources for ComponentB {

View File

@ -2,8 +2,8 @@ use bevy::{
app::{ScheduleRunnerPlugin, ScheduleRunnerSettings}, app::{ScheduleRunnerPlugin, ScheduleRunnerSettings},
log::LogPlugin, log::LogPlugin,
prelude::*, prelude::*,
utils::Duration,
}; };
use std::time::Duration;
fn main() { fn main() {
App::build() App::build()