From 1a2047c75d0c7bf595eb5f217c60092bf577acb8 Mon Sep 17 00:00:00 2001 From: Acid Date: Fri, 12 Jan 2024 23:32:44 +0100 Subject: [PATCH 1/4] Simplify conditions (cherry picked from commit d35c09fb55c2bbe27b4eabd557490ba86135363b) --- crates/bevy_ecs/src/schedule/condition.rs | 42 +++++++++++------------ crates/bevy_ecs/src/system/mod.rs | 2 +- crates/bevy_pbr/src/wireframe.rs | 4 +-- examples/ecs/run_conditions.rs | 6 ++-- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 916fc12ceaac3..ea1c1380dd55b 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -273,11 +273,11 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// ``` - pub fn resource_exists() -> impl FnMut(Option>) -> bool + Clone + pub fn resource_exists(res: Option>) -> bool where T: Resource, { - move |res: Option>| res.is_some() + res.is_some() } /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` @@ -396,11 +396,11 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// ``` - pub fn resource_added() -> impl FnMut(Option>) -> bool + Clone + pub fn resource_added(res: Option>) -> bool where T: Resource, { - move |res: Option>| match res { + match res { Some(res) => res.is_added(), None => false, } @@ -453,11 +453,11 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 51); /// ``` - pub fn resource_changed() -> impl FnMut(Res) -> bool + Clone + pub fn resource_changed(res: Res) -> bool where T: Resource, { - move |res: Res| res.is_changed() + res.is_changed() } /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` @@ -510,11 +510,11 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 51); /// ``` - pub fn resource_exists_and_changed() -> impl FnMut(Option>) -> bool + Clone + pub fn resource_exists_and_changed(res: Option>) -> bool where T: Resource, { - move |res: Option>| match res { + match res { Some(res) => res.is_changed(), None => false, } @@ -694,8 +694,8 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// ``` - pub fn state_exists() -> impl FnMut(Option>>) -> bool + Clone { - move |current_state: Option>>| current_state.is_some() + pub fn state_exists(current_state: Option>>) -> bool { + current_state.is_some() } /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` @@ -866,8 +866,8 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 2); /// ``` - pub fn state_changed() -> impl FnMut(Res>) -> bool + Clone { - move |current_state: Res>| current_state.is_changed() + pub fn state_changed(current_state: Res>) -> bool { + current_state.is_changed() } /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` @@ -947,8 +947,8 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// ``` - pub fn any_with_component() -> impl FnMut(Query<(), With>) -> bool + Clone { - move |query: Query<(), With>| !query.is_empty() + pub fn any_with_component(query: Query<(), With>) -> bool { + !query.is_empty() } /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` @@ -1199,17 +1199,17 @@ mod tests { Schedule::default().add_systems( (test_system, test_system) .distributive_run_if(run_once()) - .distributive_run_if(resource_exists::>()) - .distributive_run_if(resource_added::>()) - .distributive_run_if(resource_changed::>()) - .distributive_run_if(resource_exists_and_changed::>()) + .distributive_run_if(resource_exists::>) + .distributive_run_if(resource_added::>) + .distributive_run_if(resource_changed::>) + .distributive_run_if(resource_exists_and_changed::>) .distributive_run_if(resource_changed_or_removed::>()) .distributive_run_if(resource_removed::>()) - .distributive_run_if(state_exists::()) + .distributive_run_if(state_exists::) .distributive_run_if(in_state(TestState::A).or_else(in_state(TestState::B))) - .distributive_run_if(state_changed::()) + .distributive_run_if(state_changed::) .distributive_run_if(on_event::()) - .distributive_run_if(any_with_component::()) + .distributive_run_if(any_with_component::) .distributive_run_if(not(run_once())), ); } diff --git a/crates/bevy_ecs/src/system/mod.rs b/crates/bevy_ecs/src/system/mod.rs index d1111f18f1a08..630eb22bdff1d 100644 --- a/crates/bevy_ecs/src/system/mod.rs +++ b/crates/bevy_ecs/src/system/mod.rs @@ -1740,7 +1740,7 @@ mod tests { res.0 += 2; }, ) - .distributive_run_if(resource_exists::().or_else(resource_exists::())), + .distributive_run_if(resource_exists::.or_else(resource_exists::)), ); sched.initialize(&mut world).unwrap(); sched.run(&mut world); diff --git a/crates/bevy_pbr/src/wireframe.rs b/crates/bevy_pbr/src/wireframe.rs index dc1075090c891..e779a1668d2f9 100644 --- a/crates/bevy_pbr/src/wireframe.rs +++ b/crates/bevy_pbr/src/wireframe.rs @@ -43,10 +43,10 @@ impl Plugin for WireframePlugin { .add_systems( Update, ( - global_color_changed.run_if(resource_changed::()), + global_color_changed.run_if(resource_changed::), wireframe_color_changed, apply_wireframe_material, - apply_global_wireframe_material.run_if(resource_changed::()), + apply_global_wireframe_material.run_if(resource_changed::), ), ); } diff --git a/examples/ecs/run_conditions.rs b/examples/ecs/run_conditions.rs index 5e115e5f18baf..b2fbea9176ea5 100644 --- a/examples/ecs/run_conditions.rs +++ b/examples/ecs/run_conditions.rs @@ -17,12 +17,12 @@ fn main() { increment_input_counter // The common_conditions module has a few useful run conditions // for checking resources and states. These are included in the prelude. - .run_if(resource_exists::()) + .run_if(resource_exists::) // `.or_else()` is a run condition combinator that only evaluates the second condition // if the first condition returns `false`. This behavior is known as "short-circuiting", // and is how the `||` operator works in Rust (as well as most C-family languages). // In this case, the `has_user_input` run condition will be evaluated since the `Unused` resource has not been initialized. - .run_if(resource_exists::().or_else( + .run_if(resource_exists::.or_else( // This is a custom run condition, defined using a system that returns // a `bool` and which has read-only `SystemParam`s. // Both run conditions must return `true` in order for the system to run. @@ -34,7 +34,7 @@ fn main() { // if the first condition returns `true`, analogous to the `&&` operator. // In this case, the short-circuiting behavior prevents the second run condition from // panicking if the `InputCounter` resource has not been initialized. - .run_if(resource_exists::().and_then( + .run_if(resource_exists::.and_then( // This is a custom run condition in the form of a closure. // This is useful for small, simple run conditions you don't need to reuse. // All the normal rules still apply: all parameters must be read only except for local parameters. From a9fec7df14f30d13b608b45e0ec61b49dcc1a638 Mon Sep 17 00:00:00 2001 From: Acid Date: Sat, 13 Jan 2024 00:07:17 +0100 Subject: [PATCH 2/4] Docs fixes (cherry picked from commit 7361b05f279c243c220666bb5b9eac3c1d89b3c6) --- crates/bevy_ecs/src/change_detection.rs | 6 +++--- crates/bevy_ecs/src/schedule/condition.rs | 26 +++++++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 93ae4c1ca765c..ed3834e05fde9 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -146,7 +146,7 @@ pub trait DetectChangesMut: DetectChanges { /// } /// # let mut world = World::new(); /// # world.insert_resource(Score(1)); - /// # let mut score_changed = IntoSystem::into_system(resource_changed::()); + /// # let mut score_changed = IntoSystem::into_system(resource_changed::); /// # score_changed.initialize(&mut world); /// # score_changed.run((), &mut world); /// # @@ -210,11 +210,11 @@ pub trait DetectChangesMut: DetectChanges { /// # let mut world = World::new(); /// # world.insert_resource(Events::::default()); /// # world.insert_resource(Score(1)); - /// # let mut score_changed = IntoSystem::into_system(resource_changed::()); + /// # let mut score_changed = IntoSystem::into_system(resource_changed::); /// # score_changed.initialize(&mut world); /// # score_changed.run((), &mut world); /// # - /// # let mut score_changed_event = IntoSystem::into_system(on_event::()); + /// # let mut score_changed_event = IntoSystem::into_system(on_event::); /// # score_changed_event.initialize(&mut world); /// # score_changed_event.run((), &mut world); /// # diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index ea1c1380dd55b..24cebf993bb68 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -107,7 +107,7 @@ pub trait Condition: sealed::Condition { /// # fn my_system() {} /// app.add_systems( /// // `resource_equals` will only get run if the resource `R` exists. - /// my_system.run_if(resource_exists::().and_then(resource_equals(R(0)))), + /// my_system.run_if(resource_exists::.and_then(resource_equals(R(0)))), /// ); /// # app.run(&mut world); /// ``` @@ -145,7 +145,7 @@ pub trait Condition: sealed::Condition { /// # fn my_system(mut c: ResMut) { c.0 = true; } /// app.add_systems( /// // Only run the system if either `A` or `B` exist. - /// my_system.run_if(resource_exists::().or_else(resource_exists::())), + /// my_system.run_if(resource_exists::.or_else(resource_exists::)), /// ); /// # /// # world.insert_resource(C(false)); @@ -258,7 +258,7 @@ pub mod common_conditions { /// # let mut world = World::new(); /// app.add_systems( /// // `resource_exists` will only return true if the given resource exists in the world - /// my_system.run_if(resource_exists::()), + /// my_system.run_if(resource_exists::), /// ); /// /// fn my_system(mut counter: ResMut) { @@ -379,7 +379,7 @@ pub mod common_conditions { /// app.add_systems( /// // `resource_added` will only return true if the /// // given resource was just added - /// my_system.run_if(resource_added::()), + /// my_system.run_if(resource_added::), /// ); /// /// fn my_system(mut counter: ResMut) { @@ -431,11 +431,11 @@ pub mod common_conditions { /// // `resource_changed` will only return true if the /// // given resource was just changed (or added) /// my_system.run_if( - /// resource_changed::() + /// resource_changed:: /// // By default detecting changes will also trigger if the resource was /// // just added, this won't work with my example so I will add a second /// // condition to make sure the resource wasn't just added - /// .and_then(not(resource_added::())) + /// .and_then(not(resource_added::)) /// ), /// ); /// @@ -484,11 +484,11 @@ pub mod common_conditions { /// // `resource_exists_and_changed` will only return true if the /// // given resource exists and was just changed (or added) /// my_system.run_if( - /// resource_exists_and_changed::() + /// resource_exists_and_changed:: /// // By default detecting changes will also trigger if the resource was /// // just added, this won't work with my example so I will add a second /// // condition to make sure the resource wasn't just added - /// .and_then(not(resource_added::())) + /// .and_then(not(resource_added::)) /// ), /// ); /// @@ -546,11 +546,11 @@ pub mod common_conditions { /// // `resource_changed_or_removed` will only return true if the /// // given resource was just changed or removed (or added) /// my_system.run_if( - /// resource_changed_or_removed::() + /// resource_changed_or_removed:: /// // By default detecting changes will also trigger if the resource was /// // just added, this won't work with my example so I will add a second /// // condition to make sure the resource wasn't just added - /// .and_then(not(resource_added::())) + /// .and_then(not(resource_added::)) /// ), /// ); /// @@ -677,7 +677,7 @@ pub mod common_conditions { /// app.add_systems( /// // `state_exists` will only return true if the /// // given state exists - /// my_system.run_if(state_exists::()), + /// my_system.run_if(state_exists::), /// ); /// /// fn my_system(mut counter: ResMut) { @@ -845,7 +845,7 @@ pub mod common_conditions { /// // `state_changed` will only return true if the /// // given states value has just been updated or /// // the state has just been added - /// my_system.run_if(state_changed::()), + /// my_system.run_if(state_changed::), /// ); /// /// fn my_system(mut counter: ResMut) { @@ -927,7 +927,7 @@ pub mod common_conditions { /// # let mut world = World::new(); /// # world.init_resource::(); /// app.add_systems( - /// my_system.run_if(any_with_component::()), + /// my_system.run_if(any_with_component::), /// ); /// /// #[derive(Component)] From 2dd6a35ad5e339764f050e87f6c724fff7d90ea0 Mon Sep 17 00:00:00 2001 From: Acid Date: Sat, 13 Jan 2024 00:46:04 +0100 Subject: [PATCH 3/4] Fixed more docs tests (cherry picked from commit 1a07abd6d51cbd0810dbb4ac0e5e011a42450490) --- crates/bevy_ecs/src/change_detection.rs | 2 +- crates/bevy_ecs/src/schedule/condition.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index ed3834e05fde9..08710679a5325 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -214,7 +214,7 @@ pub trait DetectChangesMut: DetectChanges { /// # score_changed.initialize(&mut world); /// # score_changed.run((), &mut world); /// # - /// # let mut score_changed_event = IntoSystem::into_system(on_event::); + /// # let mut score_changed_event = IntoSystem::into_system(on_event::()); /// # score_changed_event.initialize(&mut world); /// # score_changed_event.run((), &mut world); /// # diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 24cebf993bb68..55c073f04a701 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -546,7 +546,7 @@ pub mod common_conditions { /// // `resource_changed_or_removed` will only return true if the /// // given resource was just changed or removed (or added) /// my_system.run_if( - /// resource_changed_or_removed:: + /// resource_changed_or_removed::() /// // By default detecting changes will also trigger if the resource was /// // just added, this won't work with my example so I will add a second /// // condition to make sure the resource wasn't just added From 2bac596a4cd17815e876787699d0dad37af554ca Mon Sep 17 00:00:00 2001 From: Acid Date: Sat, 13 Jan 2024 14:02:21 +0100 Subject: [PATCH 4/4] Update docs --- crates/bevy_ecs/src/schedule/condition.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 55c073f04a701..95173181e0d88 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -245,7 +245,7 @@ pub mod common_conditions { } } - /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` + /// A [`Condition`](super::Condition)-satisfying system that returns `true` /// if the resource exists. /// /// # Example @@ -365,7 +365,7 @@ pub mod common_conditions { } } - /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` + /// A [`Condition`](super::Condition)-satisfying system that returns `true` /// if the resource of the given type has been added since the condition was last checked. /// /// # Example @@ -406,7 +406,7 @@ pub mod common_conditions { } } - /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` + /// A [`Condition`](super::Condition)-satisfying system that returns `true` /// if the resource of the given type has had its value changed since the condition /// was last checked. /// @@ -460,7 +460,7 @@ pub mod common_conditions { res.is_changed() } - /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` + /// A [`Condition`](super::Condition)-satisfying system that returns `true` /// if the resource of the given type has had its value changed since the condition /// was last checked. /// @@ -655,7 +655,7 @@ pub mod common_conditions { } } - /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` + /// A [`Condition`](super::Condition)-satisfying system that returns `true` /// if the state machine exists. /// /// # Example @@ -813,7 +813,7 @@ pub mod common_conditions { } } - /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` + /// A [`Condition`](super::Condition)-satisfying system that returns `true` /// if the state machine changed state. /// /// To do things on transitions to/from specific states, use their respective OnEnter/OnExit @@ -914,7 +914,7 @@ pub mod common_conditions { move |mut reader: EventReader| reader.read().count() > 0 } - /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` + /// A [`Condition`](super::Condition)-satisfying system that returns `true` /// if there are any entities with the given component type. /// /// # Example