From afb33234db4b8ce6fb384cea4bad5172ac748af3 Mon Sep 17 00:00:00 2001 From: Alexander Sepity Date: Thu, 1 Jul 2021 19:09:34 +0000 Subject: [PATCH] Optional `.system()`, part 3 (#2422) # Objective - Continue work of #2398 and #2403. - Make `.system()` syntax optional when using `.config()` API. ## Solution - Introduce new prelude trait, `ConfigurableSystem`, that shorthands `my_system.system().config(...)` as `my_system.config(...)`. - Expand `configure_system_local` test to also cover the new syntax. --- crates/bevy_ecs/src/lib.rs | 4 +-- crates/bevy_ecs/src/system/function_system.rs | 36 +++++++++++++++++++ crates/bevy_ecs/src/system/mod.rs | 10 ++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index a6fdbced0894d..d95a15bd0804b 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -29,8 +29,8 @@ pub mod prelude { Schedule, Stage, StageLabel, State, SystemLabel, SystemSet, SystemStage, }, system::{ - Commands, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem, Local, NonSend, - NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System, + Commands, ConfigurableSystem, In, IntoChainSystem, IntoExclusiveSystem, IntoSystem, + Local, NonSend, NonSendMut, Query, QuerySet, RemovedComponents, Res, ResMut, System, }, world::{FromWorld, Mut, World}, }; diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 566ef78dbf9c7..a8de42abf54f9 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -263,6 +263,42 @@ impl FunctionSystem: + IntoSystem +{ + /// See [`FunctionSystem::config()`](crate::system::FunctionSystem::config). + fn config( + self, + f: impl FnOnce(&mut ::Config), + ) -> Self::System; +} + +impl ConfigurableSystem for F +where + In: 'static, + Out: 'static, + Param: SystemParam + 'static, + Marker: 'static, + F: SystemParamFunction + + IntoSystem< + In, + Out, + (IsFunctionSystem, Param, Marker), + System = FunctionSystem, + > + Send + + Sync + + 'static, +{ + fn config( + self, + f: impl FnOnce(&mut <::Fetch as SystemParamState>::Config), + ) -> Self::System { + self.system().config(f) + } +} + pub struct IsFunctionSystem; impl IntoSystem for F diff --git a/crates/bevy_ecs/src/system/mod.rs b/crates/bevy_ecs/src/system/mod.rs index c05dfc7f80b3c..2110bad7520c9 100644 --- a/crates/bevy_ecs/src/system/mod.rs +++ b/crates/bevy_ecs/src/system/mod.rs @@ -27,8 +27,8 @@ mod tests { query::{Added, Changed, Or, With, Without}, schedule::{Schedule, Stage, SystemStage}, system::{ - IntoExclusiveSystem, IntoSystem, Local, Query, QuerySet, RemovedComponents, Res, - ResMut, System, SystemState, + ConfigurableSystem, IntoExclusiveSystem, IntoSystem, Local, Query, QuerySet, + RemovedComponents, Res, ResMut, System, SystemState, }, world::{FromWorld, World}, }; @@ -372,7 +372,13 @@ mod tests { // ensure the system actually ran assert!(*world.get_resource::().unwrap()); + + // Now do the same with omitted `.system()`. + world.insert_resource(false); + run_system(&mut world, sys.config(|config| config.0 = Some(42))); + assert!(*world.get_resource::().unwrap()); } + #[test] fn world_collections_system() { let mut world = World::default();