Skip to content

Commit

Permalink
Implement Clone for CombinatorSystem (#8826)
Browse files Browse the repository at this point in the history
# Objective

Make a combined system cloneable if both systems are cloneable on their
own. This is necessary for using chained conditions (e.g
`cond1.and_then(cond2)`) with `distributive_run_if()`.

## Solution

Implement `Clone` for `CombinatorSystem<Func, A, B>` where `A, B:
Clone`.
  • Loading branch information
yyogo committed Jun 12, 2023
1 parent 2b4fc10 commit c475e27
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
11 changes: 11 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,17 @@ 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
34 changes: 33 additions & 1 deletion crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,10 @@ mod tests {
prelude::AnyOf,
query::{Added, Changed, Or, With, Without},
removal_detection::RemovedComponents,
schedule::{apply_deferred, IntoSystemConfigs, Schedule},
schedule::{
apply_deferred, common_conditions::resource_exists, Condition, IntoSystemConfigs,
Schedule,
},
system::{
adapter::new, Commands, In, IntoSystem, Local, NonSend, NonSendMut, ParamSet, Query,
QueryComponentError, Res, ResMut, Resource, System, SystemState,
Expand Down Expand Up @@ -1835,4 +1838,33 @@ 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 c475e27

Please sign in to comment.