From 12491fd2ea423a33d4f226c80b0de5080ea97c2d Mon Sep 17 00:00:00 2001 From: sleepy_swords <33922797+SleepySwords@users.noreply.github.com> Date: Thu, 13 Oct 2022 21:58:05 +1100 Subject: [PATCH 1/5] feat: Add Serialize and Deserialize derives for Timer and Stopwatch --- crates/bevy_time/Cargo.toml | 1 + crates/bevy_time/src/stopwatch.rs | 6 ++++-- crates/bevy_time/src/timer.rs | 5 +++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/bevy_time/Cargo.toml b/crates/bevy_time/Cargo.toml index 7ce9db0ff042b..4afbadbf00640 100644 --- a/crates/bevy_time/Cargo.toml +++ b/crates/bevy_time/Cargo.toml @@ -18,3 +18,4 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" } # other crossbeam-channel = "0.5.0" +serde = { version = "1", features = ["derive"] } diff --git a/crates/bevy_time/src/stopwatch.rs b/crates/bevy_time/src/stopwatch.rs index 299ec1aa936a5..021025ce71340 100644 --- a/crates/bevy_time/src/stopwatch.rs +++ b/crates/bevy_time/src/stopwatch.rs @@ -1,6 +1,8 @@ use bevy_reflect::prelude::*; use bevy_reflect::Reflect; use bevy_utils::Duration; +use serde::Deserialize; +use serde::Serialize; /// A Stopwatch is a struct that track elapsed time when started. /// @@ -23,8 +25,8 @@ use bevy_utils::Duration; /// assert!(stopwatch.paused()); /// assert_eq!(stopwatch.elapsed_secs(), 0.0); /// ``` -#[derive(Clone, Debug, Default, Reflect)] -#[reflect(Default)] +#[derive(Clone, Debug, Default, Serialize, Deserialize, Reflect)] +#[reflect(Default, Serialize, Deserialize)] pub struct Stopwatch { elapsed: Duration, paused: bool, diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 4661bbeae9ea7..acdf8e7d37070 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -1,6 +1,7 @@ use crate::Stopwatch; use bevy_reflect::prelude::*; use bevy_utils::Duration; +use serde::{Deserialize, Serialize}; /// Tracks elapsed time. Enters the finished state once `duration` is reached. /// @@ -9,8 +10,8 @@ use bevy_utils::Duration; /// exceeded, and can still be reset at any given point. /// /// Paused timers will not have elapsed time increased. -#[derive(Clone, Debug, Default, Reflect)] -#[reflect(Default)] +#[derive(Clone, Debug, Default, Serialize, Deserialize, Reflect)] +#[reflect(Default, Serialize, Deserialize)] pub struct Timer { stopwatch: Stopwatch, duration: Duration, From c959b043e3ee92f31aa31691a4da8d76b5651680 Mon Sep 17 00:00:00 2001 From: sleepy_swords <33922797+SleepySwords@users.noreply.github.com> Date: Thu, 13 Oct 2022 22:09:14 +1100 Subject: [PATCH 2/5] feat: add Serialize and Deserialize derives for Countdown + PrintOnCompletionTimer --- examples/ecs/timers.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/ecs/timers.rs b/examples/ecs/timers.rs index 9bddae5181ebb..b17c8b9e0f26b 100644 --- a/examples/ecs/timers.rs +++ b/examples/ecs/timers.rs @@ -1,6 +1,7 @@ //! Illustrates how `Timer`s can be used both as resources and components. use bevy::{log::info, prelude::*}; +use serde::{Serialize, Deserialize}; fn main() { App::new() @@ -12,10 +13,10 @@ fn main() { .run(); } -#[derive(Component, Deref, DerefMut)] +#[derive(Component, Deref, DerefMut, Serialize, Deserialize)] pub struct PrintOnCompletionTimer(Timer); -#[derive(Resource)] +#[derive(Resource, Serialize, Deserialize)] pub struct Countdown { pub percent_trigger: Timer, pub main_timer: Timer, From 27ae7e8b3ba72d8c806277579b0e2c97ab259dad Mon Sep 17 00:00:00 2001 From: sleepy_swords <33922797+SleepySwords@users.noreply.github.com> Date: Thu, 13 Oct 2022 23:26:44 +1100 Subject: [PATCH 3/5] lint: apply linting --- examples/ecs/timers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ecs/timers.rs b/examples/ecs/timers.rs index b17c8b9e0f26b..a2273efbede84 100644 --- a/examples/ecs/timers.rs +++ b/examples/ecs/timers.rs @@ -1,7 +1,7 @@ //! Illustrates how `Timer`s can be used both as resources and components. use bevy::{log::info, prelude::*}; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; fn main() { App::new() From aafcc59087ad9d7df375d3886cb0c724c4db6148 Mon Sep 17 00:00:00 2001 From: sleepy_swords <33922797+SleepySwords@users.noreply.github.com> Date: Fri, 14 Oct 2022 01:59:49 +1100 Subject: [PATCH 4/5] feat: apply requested changes --- crates/bevy_internal/Cargo.toml | 2 +- crates/bevy_time/Cargo.toml | 5 ++++- crates/bevy_time/src/stopwatch.rs | 7 +++---- crates/bevy_time/src/timer.rs | 6 +++--- examples/ecs/timers.rs | 5 ++--- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index f38c60c203f9a..3c4e0708ab948 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -45,7 +45,7 @@ wav = ["bevy_audio/wav"] # Enable watching file system for asset hot reload filesystem_watcher = ["bevy_asset/filesystem_watcher"] -serialize = ["bevy_input/serialize", "bevy_window/serialize"] +serialize = ["bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize"] # Display server protocol support (X11 is enabled by default) wayland = ["bevy_winit/wayland"] diff --git a/crates/bevy_time/Cargo.toml b/crates/bevy_time/Cargo.toml index 4afbadbf00640..80f3ea8afe507 100644 --- a/crates/bevy_time/Cargo.toml +++ b/crates/bevy_time/Cargo.toml @@ -8,6 +8,9 @@ repository = "https://github.com/bevyengine/bevy" license = "MIT OR Apache-2.0" keywords = ["bevy"] +[features] +default = [] +serialize = ["serde"] [dependencies] # bevy @@ -18,4 +21,4 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" } # other crossbeam-channel = "0.5.0" -serde = { version = "1", features = ["derive"] } +serde = { version = "1", features = ["derive"], optional = true } diff --git a/crates/bevy_time/src/stopwatch.rs b/crates/bevy_time/src/stopwatch.rs index 021025ce71340..2dd925e4efb55 100644 --- a/crates/bevy_time/src/stopwatch.rs +++ b/crates/bevy_time/src/stopwatch.rs @@ -1,8 +1,6 @@ use bevy_reflect::prelude::*; use bevy_reflect::Reflect; use bevy_utils::Duration; -use serde::Deserialize; -use serde::Serialize; /// A Stopwatch is a struct that track elapsed time when started. /// @@ -25,8 +23,9 @@ use serde::Serialize; /// assert!(stopwatch.paused()); /// assert_eq!(stopwatch.elapsed_secs(), 0.0); /// ``` -#[derive(Clone, Debug, Default, Serialize, Deserialize, Reflect)] -#[reflect(Default, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, Reflect)] +#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))] +#[reflect(Default)] pub struct Stopwatch { elapsed: Duration, paused: bool, diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index acdf8e7d37070..dc10dbec1d460 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -1,7 +1,6 @@ use crate::Stopwatch; use bevy_reflect::prelude::*; use bevy_utils::Duration; -use serde::{Deserialize, Serialize}; /// Tracks elapsed time. Enters the finished state once `duration` is reached. /// @@ -10,8 +9,9 @@ use serde::{Deserialize, Serialize}; /// exceeded, and can still be reset at any given point. /// /// Paused timers will not have elapsed time increased. -#[derive(Clone, Debug, Default, Serialize, Deserialize, Reflect)] -#[reflect(Default, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, Reflect)] +#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))] +#[reflect(Default)] pub struct Timer { stopwatch: Stopwatch, duration: Duration, diff --git a/examples/ecs/timers.rs b/examples/ecs/timers.rs index a2273efbede84..9bddae5181ebb 100644 --- a/examples/ecs/timers.rs +++ b/examples/ecs/timers.rs @@ -1,7 +1,6 @@ //! Illustrates how `Timer`s can be used both as resources and components. use bevy::{log::info, prelude::*}; -use serde::{Deserialize, Serialize}; fn main() { App::new() @@ -13,10 +12,10 @@ fn main() { .run(); } -#[derive(Component, Deref, DerefMut, Serialize, Deserialize)] +#[derive(Component, Deref, DerefMut)] pub struct PrintOnCompletionTimer(Timer); -#[derive(Resource, Serialize, Deserialize)] +#[derive(Resource)] pub struct Countdown { pub percent_trigger: Timer, pub main_timer: Timer, From 329ffaddebf51ff506d76aab6b5d500c27f3ee99 Mon Sep 17 00:00:00 2001 From: sleepy_swords <33922797+SleepySwords@users.noreply.github.com> Date: Tue, 18 Oct 2022 01:25:15 +1100 Subject: [PATCH 5/5] feat: add derive Serialize + Deserialize for TimerMode --- crates/bevy_time/src/timer.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index c311abe1e3330..767c79d91b9a7 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -402,6 +402,7 @@ impl Timer { /// Specifies [`Timer`] behavior. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Default, Reflect)] +#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))] #[reflect(Default)] pub enum TimerMode { /// Run once and stop.