From e258de8ec840b9230d5c22be3f9f5ba0a39b6780 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 9 Mar 2023 14:06:08 -0800 Subject: [PATCH 1/6] impl Deref for State --- crates/bevy_ecs/src/schedule/state.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index 326c99d2afecd..da322e7d08d69 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -85,6 +85,13 @@ pub struct OnUpdate(pub S); #[derive(Resource, Default, Debug)] pub struct State(pub S); +impl std::ops::Deref for State { + type Target = S; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + /// The next state of [`State`]. /// /// To queue a transition, just set the contained value to `Some(next_state)`. From cbbb81d5024b308d5452a27967fdd6d6dea684fd Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 9 Mar 2023 14:25:23 -0800 Subject: [PATCH 2/6] Privatize the state field so it cant be modified by accident --- crates/bevy_ecs/src/schedule/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index da322e7d08d69..21195112caf4c 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -83,7 +83,7 @@ pub struct OnUpdate(pub S); /// /// The starting state is defined via the [`Default`] implementation for `S`. #[derive(Resource, Default, Debug)] -pub struct State(pub S); +pub struct State(S); impl std::ops::Deref for State { type Target = S; From 42d8e34fbec75702989f0f56b29157d6930e9a53 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 23 Mar 2023 01:24:17 -0700 Subject: [PATCH 3/6] Getter instead of deref --- crates/bevy_ecs/src/schedule/condition.rs | 4 ++-- crates/bevy_ecs/src/schedule/state.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 0ad7f0591744c..ba7f51b4addc1 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -692,7 +692,7 @@ pub mod common_conditions { /// assert_eq!(world.resource::().0, 0); /// ``` pub fn in_state(state: S) -> impl FnMut(Res>) -> bool + Clone { - move |current_state: Res>| current_state.0 == state + move |current_state: Res>| *current_state.get() == state } /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` @@ -751,7 +751,7 @@ pub mod common_conditions { state: S, ) -> impl FnMut(Option>>) -> bool + Clone { move |current_state: Option>>| match current_state { - Some(current_state) => current_state.0 == state, + Some(current_state) => *current_state.get() == state, None => false, } } diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index 21195112caf4c..e9c349d592de4 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -85,9 +85,9 @@ pub struct OnUpdate(pub S); #[derive(Resource, Default, Debug)] pub struct State(S); -impl std::ops::Deref for State { - type Target = S; - fn deref(&self) -> &Self::Target { +impl State { + /// Get the current state. + pub fn get(&self) -> &S { &self.0 } } From e94a5fd24bfc631d0b71985ff7bb81e28d9b441a Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 23 Mar 2023 01:47:57 -0700 Subject: [PATCH 4/6] Fix up doctest --- crates/bevy_ecs/src/schedule/condition.rs | 2 +- crates/bevy_ecs/src/schedule/state.rs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index ba7f51b4addc1..8b9deed2cdf60 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -685,7 +685,7 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// - /// *world.resource_mut::>() = State(GameState::Paused); + /// *world.resource_mut::>() = State::new(GameState::Paused); /// /// // Now that we are in `GameState::Pause`, `pause_system` will run /// app.run(&mut world); diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index e9c349d592de4..1c844d54955bb 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -86,6 +86,13 @@ pub struct OnUpdate(pub S); pub struct State(S); impl State { + /// Creates a new state with a specific value. + /// + /// To change the state use [`NextState`] rather than using this to modify the `State`. + pub fn new(state: S) -> Self { + Self(state) + } + /// Get the current state. pub fn get(&self) -> &S { &self.0 From 3b7ec7d83def95db1166bb22a26f32015c723ae2 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 23 Mar 2023 02:18:01 -0700 Subject: [PATCH 5/6] Other doctests --- crates/bevy_ecs/src/schedule/condition.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 8b9deed2cdf60..26ec1cc5abbf2 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -741,7 +741,7 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// - /// *world.resource_mut::>() = State(GameState::Paused); + /// *world.resource_mut::>() = State::new(GameState::Paused); /// /// // Now that we are in `GameState::Pause`, `pause_system` will run /// app.run(&mut world); @@ -803,7 +803,7 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// - /// *world.resource_mut::>() = State(GameState::Paused); + /// *world.resource_mut::>() = State::new(GameState::Paused); /// /// // Now that `GameState` has been updated `my_system` will run /// app.run(&mut world); From a4b8c167b633c2918b2013dcded90b550d413751 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 23 Mar 2023 18:09:11 -0700 Subject: [PATCH 6/6] Impl PartialEq for State --- crates/bevy_ecs/src/schedule/condition.rs | 4 ++-- crates/bevy_ecs/src/schedule/state.rs | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 26ec1cc5abbf2..18668961c4ba1 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -692,7 +692,7 @@ pub mod common_conditions { /// assert_eq!(world.resource::().0, 0); /// ``` pub fn in_state(state: S) -> impl FnMut(Res>) -> bool + Clone { - move |current_state: Res>| *current_state.get() == state + move |current_state: Res>| *current_state == state } /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` @@ -751,7 +751,7 @@ pub mod common_conditions { state: S, ) -> impl FnMut(Option>>) -> bool + Clone { move |current_state: Option>>| match current_state { - Some(current_state) => *current_state.get() == state, + Some(current_state) => *current_state == state, None => false, } } diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index 1c844d54955bb..1e856e0c46a02 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -99,6 +99,12 @@ impl State { } } +impl PartialEq for State { + fn eq(&self, other: &S) -> bool { + self.get() == other + } +} + /// The next state of [`State`]. /// /// To queue a transition, just set the contained value to `Some(next_state)`.