Skip to content

Commit

Permalink
Remove last uses of string-labels (#5420)
Browse files Browse the repository at this point in the history
# Objective

* Related: #4341
* Remove all remaining uses of stringly-typed labels in the repo. Right now, it's just a bunch of tests and examples.
  • Loading branch information
JoJoJet committed Sep 3, 2022
1 parent cbb884c commit 697d297
Show file tree
Hide file tree
Showing 14 changed files with 514 additions and 424 deletions.
35 changes: 22 additions & 13 deletions benches/benches/bevy_ecs/scheduling/run_criteria.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy_ecs::{
component::Component,
prelude::{ParallelSystemDescriptorCoercion, Res, Resource, RunCriteriaDescriptorCoercion},
schedule::{ShouldRun, Stage, SystemStage},
schedule::{RunCriteriaLabel, ShouldRun, Stage, SystemStage},
system::Query,
world::World,
};
Expand All @@ -11,6 +11,13 @@ fn run_stage(stage: &mut SystemStage, world: &mut World) {
stage.run(world);
}

/// Labels for run criteria which either always return yes, or always return no.
#[derive(RunCriteriaLabel)]
enum Always {
Yes,
No,
}

pub fn run_criteria_yes(criterion: &mut Criterion) {
let mut world = World::new();
let mut group = criterion.benchmark_group("run_criteria/yes");
Expand Down Expand Up @@ -85,14 +92,15 @@ pub fn run_criteria_yes_with_labels(criterion: &mut Criterion) {
}
for amount in 0..21 {
let mut stage = SystemStage::parallel();
stage.add_system(empty.with_run_criteria(always_yes.label("always yes")));

stage.add_system(empty.with_run_criteria(always_yes.label(Always::Yes)));
for _ in 0..amount {
stage
.add_system(empty.with_run_criteria("always yes"))
.add_system(empty.with_run_criteria("always yes"))
.add_system(empty.with_run_criteria("always yes"))
.add_system(empty.with_run_criteria("always yes"))
.add_system(empty.with_run_criteria("always yes"));
.add_system(empty.with_run_criteria(Always::Yes))
.add_system(empty.with_run_criteria(Always::Yes))
.add_system(empty.with_run_criteria(Always::Yes))
.add_system(empty.with_run_criteria(Always::Yes))
.add_system(empty.with_run_criteria(Always::Yes));
}
// run once to initialize systems
run_stage(&mut stage, &mut world);
Expand All @@ -116,14 +124,15 @@ pub fn run_criteria_no_with_labels(criterion: &mut Criterion) {
}
for amount in 0..21 {
let mut stage = SystemStage::parallel();
stage.add_system(empty.with_run_criteria(always_no.label("always no")));

stage.add_system(empty.with_run_criteria(always_no.label(Always::No)));
for _ in 0..amount {
stage
.add_system(empty.with_run_criteria("always no"))
.add_system(empty.with_run_criteria("always no"))
.add_system(empty.with_run_criteria("always no"))
.add_system(empty.with_run_criteria("always no"))
.add_system(empty.with_run_criteria("always no"));
.add_system(empty.with_run_criteria(Always::No))
.add_system(empty.with_run_criteria(Always::No))
.add_system(empty.with_run_criteria(Always::No))
.add_system(empty.with_run_criteria(Always::No))
.add_system(empty.with_run_criteria(Always::No));
}
// run once to initialize systems
run_stage(&mut stage, &mut world);
Expand Down
24 changes: 18 additions & 6 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ impl App {
/// # use bevy_ecs::prelude::*;
/// # let mut app = App::new();
/// #
/// app.add_stage("my_stage", SystemStage::parallel());
/// #[derive(StageLabel)]
/// struct MyStage;
/// app.add_stage(MyStage, SystemStage::parallel());
/// ```
pub fn add_stage<S: Stage>(&mut self, label: impl StageLabel, stage: S) -> &mut Self {
self.schedule.add_stage(label, stage);
Expand All @@ -168,7 +170,9 @@ impl App {
/// # use bevy_ecs::prelude::*;
/// # let mut app = App::new();
/// #
/// app.add_stage_after(CoreStage::Update, "my_stage", SystemStage::parallel());
/// #[derive(StageLabel)]
/// struct MyStage;
/// app.add_stage_after(CoreStage::Update, MyStage, SystemStage::parallel());
/// ```
pub fn add_stage_after<S: Stage>(
&mut self,
Expand All @@ -190,7 +194,9 @@ impl App {
/// # use bevy_ecs::prelude::*;
/// # let mut app = App::new();
/// #
/// app.add_stage_before(CoreStage::Update, "my_stage", SystemStage::parallel());
/// #[derive(StageLabel)]
/// struct MyStage;
/// app.add_stage_before(CoreStage::Update, MyStage, SystemStage::parallel());
/// ```
pub fn add_stage_before<S: Stage>(
&mut self,
Expand All @@ -212,7 +218,9 @@ impl App {
/// # use bevy_ecs::prelude::*;
/// # let mut app = App::new();
/// #
/// app.add_startup_stage("my_startup_stage", SystemStage::parallel());
/// #[derive(StageLabel)]
/// struct MyStartupStage;
/// app.add_startup_stage(MyStartupStage, SystemStage::parallel());
/// ```
pub fn add_startup_stage<S: Stage>(&mut self, label: impl StageLabel, stage: S) -> &mut Self {
self.schedule
Expand All @@ -234,9 +242,11 @@ impl App {
/// # use bevy_ecs::prelude::*;
/// # let mut app = App::new();
/// #
/// #[derive(StageLabel)]
/// struct MyStartupStage;
/// app.add_startup_stage_after(
/// StartupStage::Startup,
/// "my_startup_stage",
/// MyStartupStage,
/// SystemStage::parallel()
/// );
/// ```
Expand Down Expand Up @@ -265,9 +275,11 @@ impl App {
/// # use bevy_ecs::prelude::*;
/// # let mut app = App::new();
/// #
/// #[derive(StageLabel)]
/// struct MyStartupStage;
/// app.add_startup_stage_before(
/// StartupStage::Startup,
/// "my_startup_stage",
/// MyStartupStage,
/// SystemStage::parallel()
/// );
/// ```
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,13 @@ fn main() {
// Create a new Schedule, which defines an execution strategy for Systems
let mut schedule = Schedule::default();

// Define a unique public name for a new Stage.
#[derive(StageLabel)]
pub struct UpdateLabel;

// Add a Stage to our schedule. Each Stage in a schedule runs all of its systems
// before moving on to the next Stage
schedule.add_stage("update", SystemStage::parallel()
schedule.add_stage(UpdateLabel, SystemStage::parallel()
.with_system(movement)
);

Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_ecs/examples/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ fn main() {
update.add_system(remove_old_entities.after(SimulationSystem::Age));
update.add_system(print_changed_entities.after(SimulationSystem::Age));
// Add the Stage with our systems to the Schedule
schedule.add_stage("update", update);
#[derive(StageLabel)]
struct Update;
schedule.add_stage(Update, update);

// Simulate 10 frames in our world
for iteration in 1..=10 {
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_ecs/examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@ fn main() {
// Create a schedule and a stage
let mut schedule = Schedule::default();

#[derive(StageLabel)]
enum Stages {
First,
Second,
}

// Events need to be updated in every frame. This update should happen before we use
// the events. To guarantee this, we can let the update run in an earlier stage than our logic.
// Here we will use a stage called "first" that will always run it's systems before the Stage
// called "second". In "first" we update the events and in "second" we run our systems
// sending and receiving events.
let mut first = SystemStage::parallel();
first.add_system(Events::<MyEvent>::update_system);
schedule.add_stage("first", first);
schedule.add_stage(Stages::First, first);

// Add systems sending and receiving events to a "second" Stage
let mut second = SystemStage::parallel();
second.add_system(sending_system);
second.add_system(receiving_system.after(sending_system));

// Run the "second" Stage after the "first" Stage, so our Events always get updated before we use them
schedule.add_stage_after("first", "second", second);
schedule.add_stage_after(Stages::First, Stages::Second, second);

// Simulate 10 frames of our world
for iteration in 1..=10 {
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_ecs/examples/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ fn main() {
// Add systems to increase the counter and to print out the current value
update.add_system(increase_counter);
update.add_system(print_counter.after(increase_counter));
schedule.add_stage("update", update);

// Declare a unique label for the stage.
#[derive(StageLabel)]
struct Update;

// Add the stage to the schedule.
schedule.add_stage(Update, update);

for iteration in 1..=10 {
println!("Simulating frame {}/10", iteration);
Expand Down
56 changes: 41 additions & 15 deletions crates/bevy_ecs/src/schedule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ impl Schedule {
/// # use bevy_ecs::prelude::*;
/// #
/// # let mut schedule = Schedule::default();
/// schedule.add_stage("my_stage", SystemStage::parallel());
/// // Define a new label for the stage.
/// #[derive(StageLabel)]
/// struct MyStage;
/// // Add a stage with that label to the schedule.
/// schedule.add_stage(MyStage, SystemStage::parallel());
/// ```
pub fn add_stage<S: Stage>(&mut self, label: impl StageLabel, stage: S) -> &mut Self {
let label = label.as_label();
Expand All @@ -124,8 +128,14 @@ impl Schedule {
/// # use bevy_ecs::prelude::*;
/// #
/// # let mut schedule = Schedule::default();
/// # schedule.add_stage("target_stage", SystemStage::parallel());
/// schedule.add_stage_after("target_stage", "my_stage", SystemStage::parallel());
/// # #[derive(StageLabel)]
/// # struct TargetStage;
/// # schedule.add_stage(TargetStage, SystemStage::parallel());
/// // Define a new label for the stage.
/// #[derive(StageLabel)]
/// struct NewStage;
/// // Add a stage with that label to the schedule.
/// schedule.add_stage_after(TargetStage, NewStage, SystemStage::parallel());
/// ```
pub fn add_stage_after<S: Stage>(
&mut self,
Expand Down Expand Up @@ -157,9 +167,15 @@ impl Schedule {
/// # use bevy_ecs::prelude::*;
/// #
/// # let mut schedule = Schedule::default();
/// # schedule.add_stage("target_stage", SystemStage::parallel());
/// # #[derive(StageLabel)]
/// # struct TargetStage;
/// # schedule.add_stage(TargetStage, SystemStage::parallel());
/// #
/// schedule.add_stage_before("target_stage", "my_stage", SystemStage::parallel());
/// // Define a new, private label for the stage.
/// #[derive(StageLabel)]
/// struct NewStage;
/// // Add a stage with that label to the schedule.
/// schedule.add_stage_before(TargetStage, NewStage, SystemStage::parallel());
/// ```
pub fn add_stage_before<S: Stage>(
&mut self,
Expand Down Expand Up @@ -192,9 +208,11 @@ impl Schedule {
/// #
/// # fn my_system() {}
/// # let mut schedule = Schedule::default();
/// # schedule.add_stage("my_stage", SystemStage::parallel());
/// # #[derive(StageLabel)]
/// # struct MyStage;
/// # schedule.add_stage(MyStage, SystemStage::parallel());
/// #
/// schedule.add_system_to_stage("my_stage", my_system);
/// schedule.add_system_to_stage(MyStage, my_system);
/// ```
pub fn add_system_to_stage<Params>(
&mut self,
Expand Down Expand Up @@ -228,10 +246,12 @@ impl Schedule {
/// #
/// # fn my_system() {}
/// # let mut schedule = Schedule::default();
/// # schedule.add_stage("my_stage", SystemStage::parallel());
/// # #[derive(StageLabel)]
/// # struct MyStage;
/// # schedule.add_stage(MyStage, SystemStage::parallel());
/// #
/// schedule.add_system_set_to_stage(
/// "my_stage",
/// MyStage,
/// SystemSet::new()
/// .with_system(system_a)
/// .with_system(system_b)
Expand Down Expand Up @@ -265,9 +285,11 @@ impl Schedule {
/// # use bevy_ecs::prelude::*;
/// #
/// # let mut schedule = Schedule::default();
/// # schedule.add_stage("my_stage", SystemStage::parallel());
/// # #[derive(StageLabel)]
/// # struct MyStage;
/// # schedule.add_stage(MyStage, SystemStage::parallel());
/// #
/// schedule.stage("my_stage", |stage: &mut SystemStage| {
/// schedule.stage(MyStage, |stage: &mut SystemStage| {
/// stage.add_system(my_system)
/// });
/// #
Expand Down Expand Up @@ -301,9 +323,11 @@ impl Schedule {
/// #
/// # fn my_system() {}
/// # let mut schedule = Schedule::default();
/// # schedule.add_stage("my_stage", SystemStage::parallel());
/// # #[derive(StageLabel)]
/// # struct MyStage;
/// # schedule.add_stage(MyStage, SystemStage::parallel());
/// #
/// let stage = schedule.get_stage::<SystemStage>("my_stage").unwrap();
/// let stage = schedule.get_stage::<SystemStage>(MyStage).unwrap();
/// ```
pub fn get_stage<T: Stage>(&self, stage_label: impl StageLabel) -> Option<&T> {
let label = stage_label.as_label();
Expand All @@ -323,9 +347,11 @@ impl Schedule {
/// #
/// # fn my_system() {}
/// # let mut schedule = Schedule::default();
/// # schedule.add_stage("my_stage", SystemStage::parallel());
/// # #[derive(StageLabel)]
/// # struct MyStage;
/// # schedule.add_stage(MyStage, SystemStage::parallel());
/// #
/// let stage = schedule.get_stage_mut::<SystemStage>("my_stage").unwrap();
/// let stage = schedule.get_stage_mut::<SystemStage>(MyStage).unwrap();
/// ```
pub fn get_stage_mut<T: Stage>(&mut self, stage_label: impl StageLabel) -> Option<&mut T> {
let label = stage_label.as_label();
Expand Down
Loading

0 comments on commit 697d297

Please sign in to comment.