From 31df93d8891db7ec3b14cdb5c2d36778f4684ee0 Mon Sep 17 00:00:00 2001 From: Alexander Sepity Date: Thu, 8 Jul 2021 07:18:00 +0000 Subject: [PATCH] Optional `.system()`, part 4 (run criteria) (#2431) # Objective - Continue work of #2398 and friends. - Make `.system()` optional in run criteria APIs. ## Solution - Slight change to `RunCriteriaDescriptorCoercion` signature and implementors. - Implement `IntoRunCriteria` for `IntoSystem` rather than `System`. - Remove some usages of `.system()` with run criteria in tests of `stage.rs`, to verify the implementation. --- crates/bevy_ecs/src/schedule/run_criteria.rs | 28 +++++++++++--------- crates/bevy_ecs/src/schedule/stage.rs | 6 ++--- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/run_criteria.rs b/crates/bevy_ecs/src/schedule/run_criteria.rs index 5ea8fab4d1e5f1..f9f7edf3b2ef89 100644 --- a/crates/bevy_ecs/src/schedule/run_criteria.rs +++ b/crates/bevy_ecs/src/schedule/run_criteria.rs @@ -3,7 +3,7 @@ use crate::{ component::ComponentId, query::Access, schedule::{BoxedRunCriteriaLabel, GraphNode, RunCriteriaLabel}, - system::{BoxedSystem, System, SystemId}, + system::{BoxedSystem, IntoSystem, System, SystemId}, world::World, }; use std::borrow::Cow; @@ -197,12 +197,14 @@ impl IntoRunCriteria> for BoxedSystem<(), ShouldRun> } } -impl IntoRunCriteria> for S +impl IntoRunCriteria<(BoxedSystem<(), ShouldRun>, Param)> for S where - S: System, + S: IntoSystem<(), ShouldRun, Param>, { fn into(self) -> RunCriteriaDescriptorOrLabel { - RunCriteriaDescriptorOrLabel::Descriptor(new_run_criteria_descriptor(Box::new(self))) + RunCriteriaDescriptorOrLabel::Descriptor(new_run_criteria_descriptor(Box::new( + self.system(), + ))) } } @@ -227,7 +229,7 @@ impl IntoRunCriteria for RunCriteria { } } -pub trait RunCriteriaDescriptorCoercion { +pub trait RunCriteriaDescriptorCoercion { /// Assigns a label to the criteria. Must be unique. fn label(self, label: impl RunCriteriaLabel) -> RunCriteriaDescriptor; @@ -242,7 +244,7 @@ pub trait RunCriteriaDescriptorCoercion { fn after(self, label: impl RunCriteriaLabel) -> RunCriteriaDescriptor; } -impl RunCriteriaDescriptorCoercion for RunCriteriaDescriptor { +impl RunCriteriaDescriptorCoercion<()> for RunCriteriaDescriptor { fn label(mut self, label: impl RunCriteriaLabel) -> RunCriteriaDescriptor { self.label = Some(Box::new(label)); self.duplicate_label_strategy = DuplicateLabelStrategy::Panic; @@ -276,7 +278,7 @@ fn new_run_criteria_descriptor(system: BoxedSystem<(), ShouldRun>) -> RunCriteri } } -impl RunCriteriaDescriptorCoercion for BoxedSystem<(), ShouldRun> { +impl RunCriteriaDescriptorCoercion<()> for BoxedSystem<(), ShouldRun> { fn label(self, label: impl RunCriteriaLabel) -> RunCriteriaDescriptor { new_run_criteria_descriptor(self).label(label) } @@ -294,24 +296,24 @@ impl RunCriteriaDescriptorCoercion for BoxedSystem<(), ShouldRun> { } } -impl RunCriteriaDescriptorCoercion for S +impl RunCriteriaDescriptorCoercion for S where - S: System, + S: IntoSystem<(), ShouldRun, Param>, { fn label(self, label: impl RunCriteriaLabel) -> RunCriteriaDescriptor { - new_run_criteria_descriptor(Box::new(self)).label(label) + new_run_criteria_descriptor(Box::new(self.system())).label(label) } fn label_discard_if_duplicate(self, label: impl RunCriteriaLabel) -> RunCriteriaDescriptor { - new_run_criteria_descriptor(Box::new(self)).label_discard_if_duplicate(label) + new_run_criteria_descriptor(Box::new(self.system())).label_discard_if_duplicate(label) } fn before(self, label: impl RunCriteriaLabel) -> RunCriteriaDescriptor { - new_run_criteria_descriptor(Box::new(self)).before(label) + new_run_criteria_descriptor(Box::new(self.system())).before(label) } fn after(self, label: impl RunCriteriaLabel) -> RunCriteriaDescriptor { - new_run_criteria_descriptor(Box::new(self)).after(label) + new_run_criteria_descriptor(Box::new(self.system())).after(label) } } diff --git a/crates/bevy_ecs/src/schedule/stage.rs b/crates/bevy_ecs/src/schedule/stage.rs index 9cd49f828c61ff..18b3b323b77db8 100644 --- a/crates/bevy_ecs/src/schedule/stage.rs +++ b/crates/bevy_ecs/src/schedule/stage.rs @@ -1170,7 +1170,7 @@ mod tests { .with_system(make_exclusive(0).exclusive_system().before("1")) .with_system_set( SystemSet::new() - .with_run_criteria(every_other_time.system()) + .with_run_criteria(every_other_time) .with_system(make_exclusive(1).exclusive_system().label("1")), ) .with_system(make_exclusive(2).exclusive_system().after("1")); @@ -1392,7 +1392,7 @@ mod tests { make_parallel!(0) .system() .label("0") - .with_run_criteria(every_other_time.system()), + .with_run_criteria(every_other_time), ) .with_system(make_parallel!(1).system().after("0")); stage.run(&mut world); @@ -1427,7 +1427,7 @@ mod tests { // Reusing criteria. world.get_resource_mut::>().unwrap().clear(); let mut stage = SystemStage::parallel() - .with_system_run_criteria(every_other_time.system().label("every other time")) + .with_system_run_criteria(every_other_time.label("every other time")) .with_system(make_parallel!(0).system().before("1")) .with_system( make_parallel!(1)