From 9d804a231e99f0ea2f0825d89285be98d668f138 Mon Sep 17 00:00:00 2001 From: st0rmbtw <61053971+st0rmbtw@users.noreply.github.com> Date: Sun, 27 Aug 2023 20:53:37 +0300 Subject: [PATCH] Make `run_if_inner` public and rename to `run_if_dyn` (#9576) # Objective Sometimes you want to create a plugin with a custom run condition. In a function, you take the `Condition` trait and then make a `BoxedCondition` from it to store it. And then you want to add that condition to a system, but you can't, because there is only the `run_if` function available which takes `impl Condition` instead of `BoxedCondition`. So you have to create a wrapper type for the `BoxedCondition` and implement the `System` and `ReadOnlySystem` traits for the wrapper (Like it's done in the picture below). It's very inconvenient and boilerplate. But there is an easy solution for that: make the `run_if_inner` system that takes a `BoxedCondition` public. Also, it makes sense to make `in_set_inner` function public as well with the same motivation. ![image](https://github.com/bevyengine/bevy/assets/61053971/a4455180-7e0c-4c2b-9372-cd8b4a9e682e) A chunk of the source code of the `bevy-inspector-egui` crate. ## Solution Make `run_if_inner` function public. Rename `run_if_inner` to `run_if_dyn`. Make `in_set_inner` function public. Rename `in_set_inner` to `in_set_dyn`. ## Changelog Changed visibility of `run_if_inner` from `pub(crate)` to `pub`. Renamed `run_if_inner` to `run_if_dyn`. Changed visibility of `in_set_inner` from `pub(crate)` to `pub`. Renamed `in_set_inner` to `in_set_dyn`. ## Migration Guide --------- Co-authored-by: Alice Cecile Co-authored-by: Joseph <21144246+JoJoJet@users.noreply.github.com> --- crates/bevy_ecs/src/schedule/config.rs | 15 ++++++++++----- crates/bevy_ecs/src/schedule/schedule.rs | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/config.rs b/crates/bevy_ecs/src/schedule/config.rs index 708661035ed55..47b72232b1179 100644 --- a/crates/bevy_ecs/src/schedule/config.rs +++ b/crates/bevy_ecs/src/schedule/config.rs @@ -83,14 +83,15 @@ impl SystemConfigs { }) } - pub(crate) fn in_set_inner(&mut self, set: BoxedSystemSet) { + /// Adds a new boxed system set to the systems. + pub fn in_set_dyn(&mut self, set: BoxedSystemSet) { match self { SystemConfigs::SystemConfig(config) => { config.graph_info.sets.push(set); } SystemConfigs::Configs { configs, .. } => { for config in configs { - config.in_set_inner(set.dyn_clone()); + config.in_set_dyn(set.dyn_clone()); } } } @@ -167,7 +168,11 @@ impl SystemConfigs { } } - pub(crate) fn run_if_inner(&mut self, condition: BoxedCondition) { + /// Adds a new boxed run condition to the systems. + /// + /// This is useful if you have a run condition whose concrete type is unknown. + /// Prefer `run_if` for run conditions whose type is known at compile time. + pub fn run_if_dyn(&mut self, condition: BoxedCondition) { match self { SystemConfigs::SystemConfig(config) => { config.conditions.push(condition); @@ -307,7 +312,7 @@ impl IntoSystemConfigs<()> for SystemConfigs { "adding arbitrary systems to a system type set is not allowed" ); - self.in_set_inner(set.dyn_clone()); + self.in_set_dyn(set.dyn_clone()); self } @@ -341,7 +346,7 @@ impl IntoSystemConfigs<()> for SystemConfigs { } fn run_if(mut self, condition: impl Condition) -> SystemConfigs { - self.run_if_inner(new_condition(condition)); + self.run_if_dyn(new_condition(condition)); self } diff --git a/crates/bevy_ecs/src/schedule/schedule.rs b/crates/bevy_ecs/src/schedule/schedule.rs index 4f1b90ff9050d..18e230114e69d 100644 --- a/crates/bevy_ecs/src/schedule/schedule.rs +++ b/crates/bevy_ecs/src/schedule/schedule.rs @@ -532,14 +532,14 @@ impl ScheduleGraph { if more_than_one_entry { let set = AnonymousSet::new(); for config in &mut configs { - config.in_set_inner(set.dyn_clone()); + config.in_set_dyn(set.dyn_clone()); } let mut set_config = set.into_config(); set_config.conditions.extend(collective_conditions); self.configure_set(set_config); } else { for condition in collective_conditions { - configs[0].run_if_inner(condition); + configs[0].run_if_dyn(condition); } } }