From fc9368752f3d2a4b136d6b33cdf452ac5907a630 Mon Sep 17 00:00:00 2001 From: Jonathan Goren Date: Mon, 12 Jun 2023 10:33:29 +0300 Subject: [PATCH] Implement `Clone` for `CombinatorSystem` Where `A, B: Clone`. Needed in order to use chained conditions with `distributive_run_if`. --- crates/bevy_ecs/src/system/combinator.rs | 12 ++++++++++++ crates/bevy_ecs/src/system/mod.rs | 25 +++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/combinator.rs b/crates/bevy_ecs/src/system/combinator.rs index 4a96296c82700d..c0a15d881b53f9 100644 --- a/crates/bevy_ecs/src/system/combinator.rs +++ b/crates/bevy_ecs/src/system/combinator.rs @@ -242,6 +242,18 @@ where { } + +impl Clone for CombinatorSystem +where + A: Clone, + B: Clone, +{ + /// Clone the combined system. The cloned instance must be `.initialize()`d before it can run. + fn clone(&self) -> Self { + CombinatorSystem::new(self.a.clone(), self.b.clone(), self.name.clone()) + } +} + /// A [`System`] created by piping the output of the first system into the input of the second. /// /// This can be repeated indefinitely, but system pipes cannot branch: the output is consumed by the receiving system. diff --git a/crates/bevy_ecs/src/system/mod.rs b/crates/bevy_ecs/src/system/mod.rs index 1bf773eb99fed8..1ea1be55494917 100644 --- a/crates/bevy_ecs/src/system/mod.rs +++ b/crates/bevy_ecs/src/system/mod.rs @@ -493,12 +493,13 @@ mod tests { prelude::AnyOf, query::{Added, Changed, Or, With, Without}, removal_detection::RemovedComponents, - schedule::{apply_deferred, IntoSystemConfigs, Schedule}, + schedule::{apply_deferred, IntoSystemConfigs, Schedule, Condition, common_conditions::resource_exists}, system::{ adapter::new, Commands, In, IntoSystem, Local, NonSend, NonSendMut, ParamSet, Query, QueryComponentError, Res, ResMut, Resource, System, SystemState, }, world::{FromWorld, World}, + }; #[derive(Resource, PartialEq, Debug)] @@ -1835,4 +1836,26 @@ mod tests { assert!(info2.first_flag); assert!(!info2.second_flag); } + + #[test] + fn test_combinator_clone() { + let mut world = World::new(); + #[derive(Resource)] + struct A; + #[derive(Resource)] + struct B; + #[derive(Resource, PartialEq, Eq, Debug)] + struct C(i32); + + world.insert_resource(A); + world.insert_resource(C(0)); + let mut sched = Schedule::new(); + sched.add_systems(( + (|mut res: ResMut| {res.0 += 1;}), + (|mut res: ResMut| {res.0 += 2;}), + ).distributive_run_if(resource_exists::().or_else(resource_exists::()))); + sched.initialize(&mut world).unwrap(); + sched.run(&mut world); + assert_eq!(world.get_resource(), Some(&C(3))); + } }