Skip to content

Commit

Permalink
use wasm-friendly instant::Instant everywhere (bevyengine#895)
Browse files Browse the repository at this point in the history
* use instant::Instant everywhere
* reexport instant::{Duration, Instant} from bevy_utils
  • Loading branch information
mrk-its authored Nov 22, 2020
1 parent d458406 commit d96493a
Show file tree
Hide file tree
Showing 20 changed files with 29 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ apk_label = "Bevy Example"
assets = "assets"
res = "assets/android-res"
icon = "@mipmap/ic_launcher"
build_targets = ["aarch64-linux-android", "armv7-linux-androideabi"]
build_targets = ["aarch64-linux-android", "armv7-linux-androideabi"]
min_sdk_version = 16
target_sdk_version = 29

1 change: 0 additions & 1 deletion crates/bevy_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ serde = { version = "1.0", features = ["derive"] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3", features = [ "Window" ] }
instant = { version = "0.1", features = ["wasm-bindgen"] }
9 changes: 2 additions & 7 deletions crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ use crate::{
event::{EventReader, Events},
plugin::Plugin,
};
use std::time::Duration;

#[cfg(target_arch = "wasm32")]
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use std::{thread, time::Instant};
use bevy_utils::{Duration, Instant};

#[cfg(target_arch = "wasm32")]
use std::{cell::RefCell, rc::Rc};
Expand Down Expand Up @@ -104,7 +99,7 @@ impl Plugin for ScheduleRunnerPlugin {
{
while let Ok(delay) = tick(&mut app, wait) {
if let Some(delay) = delay {
thread::sleep(delay);
std::thread::sleep(delay);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions crates/bevy_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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_utils = { path = "../bevy_utils", 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"] }
7 changes: 1 addition & 6 deletions crates/bevy_core/src/time/time.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use bevy_ecs::ResMut;
use std::time::Duration;

#[cfg(target_arch = "wasm32")]
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use bevy_utils::{Duration, Instant};

/// Tracks elapsed time since the last update and since the App has started
#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core/src/time/timer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::time::Time;
use bevy_ecs::prelude::*;
use bevy_property::Properties;
use std::time::Duration;
use bevy_utils::Duration;

/// Tracks elapsed time. Enters the finished state once `duration` is reached.
///
Expand Down
3 changes: 0 additions & 3 deletions crates/bevy_diagnostic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,3 @@ bevy_utils = { path = "../bevy_utils", version = "0.3.0" }
# other
uuid = { version = "0.8", features = ["v4", "serde"] }
parking_lot = "0.11.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = { version = "0.1", features = ["wasm-bindgen"] }
13 changes: 5 additions & 8 deletions crates/bevy_diagnostic/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use bevy_utils::HashMap;
use std::{
collections::VecDeque,
time::{Duration, SystemTime},
};
use bevy_utils::{Duration, HashMap, Instant};
use std::collections::VecDeque;
use uuid::Uuid;

/// Unique identifier for a [Diagnostic]
Expand All @@ -24,7 +21,7 @@ impl Default for DiagnosticId {
/// A single measurement of a [Diagnostic]
#[derive(Debug)]
pub struct DiagnosticMeasurement {
pub time: SystemTime,
pub time: Instant,
pub value: f64,
}

Expand All @@ -41,7 +38,7 @@ pub struct Diagnostic {

impl Diagnostic {
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 let Some(removed_diagnostic) = self.history.pop_back() {
self.sum -= removed_diagnostic.value;
Expand Down Expand Up @@ -90,7 +87,7 @@ impl Diagnostic {

if let Some(oldest) = self.history.back() {
if let Some(newest) = self.history.front() {
return newest.time.duration_since(oldest.time).ok();
return Some(newest.time.duration_since(oldest.time));
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_diagnostic/src/print_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_app::prelude::*;
use bevy_core::{Time, Timer};
use bevy_ecs::{Res, ResMut};
use std::time::Duration;
use bevy_utils::Duration;

/// An App Plugin that prints diagnostics to the console
pub struct PrintDiagnosticsPlugin {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ futures-lite = "1.4.0"
event-listener = "2.4.0"
async-executor = "1.3.0"
async-channel = "1.4.2"
instant = { version = "0.1", features = ["wasm-bindgen"] }
num_cpus = "1"
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"
8 changes: 4 additions & 4 deletions crates/bevy_tasks/examples/busy_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ fn main() {
.num_threads(4)
.build();

let t0 = std::time::Instant::now();
let t0 = instant::Instant::now();
pool.scope(|s| {
for i in 0..40 {
s.spawn(async move {
let now = std::time::Instant::now();
while std::time::Instant::now() - now < std::time::Duration::from_millis(100) {
let now = instant::Instant::now();
while instant::Instant::now() - now < instant::Duration::from_millis(100) {
// spin, simulating work being done
}

Expand All @@ -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());
}
4 changes: 2 additions & 2 deletions crates/bevy_tasks/examples/idle_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ fn main() {
for i in 0..1 {
s.spawn(async move {
println!("Blocking for 10 seconds");
let now = std::time::Instant::now();
while std::time::Instant::now() - now < std::time::Duration::from_millis(10000) {
let now = instant::Instant::now();
while instant::Instant::now() - now < instant::Duration::from_millis(10000) {
// spin, simulating work being done
}

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_tasks/src/countdown_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn countdown_event_ready() {
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)
std::thread::sleep(std::time::Duration::from_millis(100));
std::thread::sleep(instant::Duration::from_millis(100));

countdown_event.decrement();
handle.join().unwrap();
Expand All @@ -121,7 +121,7 @@ pub fn event_resets_if_listeners_are_cleared() {
// Verify that we are still blocked
assert_eq!(
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
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ keywords = ["bevy"]
[dependencies]
ahash = "0.5.3"
tracing = {version = "0.1", features = ["release_max_level_info"]}
instant = { version = "0.1", features = ["wasm-bindgen"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = {version = "0.2.0", features = ["js"]}
4 changes: 2 additions & 2 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use ahash::AHasher;
use ahash::RandomState;
pub use instant::{Duration, Instant};
use std::{future::Future, pin::Pin};

pub use ahash::AHasher;
pub use tracing;

#[cfg(not(target_arch = "wasm32"))]
Expand Down
3 changes: 1 addition & 2 deletions examples/app/headless.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bevy::{app::ScheduleRunnerSettings, prelude::*};
use std::time::Duration;
use bevy::{app::ScheduleRunnerSettings, prelude::*, utils::Duration};

// 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
Expand Down
3 changes: 1 addition & 2 deletions examples/app/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bevy::prelude::*;
use std::time::Duration;
use bevy::{prelude::*, utils::Duration};

/// 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).
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/ecs_guide.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bevy::{
app::{AppExit, ScheduleRunnerPlugin, ScheduleRunnerSettings},
prelude::*,
utils::Duration,
};
use rand::random;
use std::time::Duration;

/// 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!
Expand Down
4 changes: 2 additions & 2 deletions examples/scene/scene.rs
Original file line number Diff line number Diff line change
@@ -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
fn main() {
Expand Down Expand Up @@ -37,7 +37,7 @@ struct ComponentA {
struct ComponentB {
pub value: String,
#[property(ignore)]
pub time_since_startup: std::time::Duration,
pub time_since_startup: Duration,
}

impl FromResources for ComponentB {
Expand Down
2 changes: 1 addition & 1 deletion examples/wasm/headless_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use bevy::{
app::{ScheduleRunnerPlugin, ScheduleRunnerSettings},
log::LogPlugin,
prelude::*,
utils::Duration,
};
use std::time::Duration;

fn main() {
App::build()
Expand Down

0 comments on commit d96493a

Please sign in to comment.