Skip to content

Commit

Permalink
Implement Clone for CombinatorSystem
Browse files Browse the repository at this point in the history
Where `A, B: Clone`. Needed in order to use chained conditions with
`distributive_run_if`.
  • Loading branch information
yyogo committed Jun 12, 2023
1 parent 527d3a5 commit fc93687
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions crates/bevy_ecs/src/system/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ where
{
}


impl<Func, A, B> Clone for CombinatorSystem<Func, A, B>
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.
Expand Down
25 changes: 24 additions & 1 deletion crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<C>| {res.0 += 1;}),
(|mut res: ResMut<C>| {res.0 += 2;}),
).distributive_run_if(resource_exists::<A>().or_else(resource_exists::<B>())));
sched.initialize(&mut world).unwrap();
sched.run(&mut world);
assert_eq!(world.get_resource(), Some(&C(3)));
}
}

0 comments on commit fc93687

Please sign in to comment.