Skip to content

Commit

Permalink
Optional .system(), part 4 (run criteria) (#2431)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
Ratysz committed Jul 8, 2021
1 parent bc3f80f commit 11485de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
28 changes: 15 additions & 13 deletions crates/bevy_ecs/src/schedule/run_criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -197,12 +197,14 @@ impl IntoRunCriteria<BoxedSystem<(), ShouldRun>> for BoxedSystem<(), ShouldRun>
}
}

impl<S> IntoRunCriteria<BoxedSystem<(), ShouldRun>> for S
impl<S, Param> IntoRunCriteria<(BoxedSystem<(), ShouldRun>, Param)> for S
where
S: System<In = (), Out = ShouldRun>,
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(),
)))
}
}

Expand All @@ -227,7 +229,7 @@ impl IntoRunCriteria<RunCriteria> for RunCriteria {
}
}

pub trait RunCriteriaDescriptorCoercion {
pub trait RunCriteriaDescriptorCoercion<Param> {
/// Assigns a label to the criteria. Must be unique.
fn label(self, label: impl RunCriteriaLabel) -> RunCriteriaDescriptor;

Expand All @@ -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;
Expand Down Expand Up @@ -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)
}
Expand All @@ -294,24 +296,24 @@ impl RunCriteriaDescriptorCoercion for BoxedSystem<(), ShouldRun> {
}
}

impl<S> RunCriteriaDescriptorCoercion for S
impl<S, Param> RunCriteriaDescriptorCoercion<Param> for S
where
S: System<In = (), Out = ShouldRun>,
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)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/schedule/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1427,7 +1427,7 @@ mod tests {
// Reusing criteria.
world.get_resource_mut::<Vec<usize>>().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)
Expand Down

0 comments on commit 11485de

Please sign in to comment.