Remove last uses of string-labels (#5420)

# 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.
This commit is contained in:
JoJoJet 2022-09-03 18:06:41 +00:00
parent cbb884cb02
commit 697d297b55
14 changed files with 514 additions and 424 deletions

View File

@ -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,
};
@ -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");
@ -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);
@ -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);

View File

@ -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);
@ -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,
@ -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,
@ -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
@ -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()
/// );
/// ```
@ -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()
/// );
/// ```

View File

@ -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)
);

View File

@ -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 {

View File

@ -10,6 +10,12 @@ 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
@ -17,7 +23,7 @@ fn main() {
// 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();
@ -25,7 +31,7 @@ fn main() {
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 {

View File

@ -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);

View File

@ -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();
@ -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,
@ -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,
@ -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,
@ -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)
@ -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)
/// });
/// #
@ -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();
@ -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();

File diff suppressed because it is too large Load Diff

View File

@ -501,6 +501,12 @@ mod test {
let mut stage = SystemStage::parallel();
#[derive(SystemLabel)]
enum Inactive {
S4,
S5,
}
stage.add_system_set(State::<MyState>::get_driver());
stage
.add_system_set(
@ -548,7 +554,7 @@ mod test {
},
))
.add_system_set(State::on_inactive_update_set(MyState::S4).with_system(
(|mut r: ResMut<NameList>| r.0.push("inactive S4")).label("inactive s4"),
(|mut r: ResMut<NameList>| r.0.push("inactive S4")).label(Inactive::S4),
))
.add_system_set(
State::on_update_set(MyState::S5).with_system(
@ -556,14 +562,14 @@ mod test {
r.0.push("update S5");
s.overwrite_push(MyState::S6).unwrap();
})
.after("inactive s4"),
.after(Inactive::S4),
),
)
.add_system_set(
State::on_inactive_update_set(MyState::S5).with_system(
(|mut r: ResMut<NameList>| r.0.push("inactive S5"))
.label("inactive s5")
.after("inactive s4"),
.label(Inactive::S5)
.after(Inactive::S4),
),
)
.add_system_set(
@ -572,7 +578,7 @@ mod test {
r.0.push("update S6");
s.overwrite_push(MyState::Final).unwrap();
})
.after("inactive s5"),
.after(Inactive::S5),
),
)
.add_system_set(

View File

@ -130,6 +130,8 @@ pub fn assert_is_exclusive_system<Params, SystemType>(
mod tests {
use std::any::TypeId;
use crate::prelude::StageLabel;
use crate::{
self as bevy_ecs,
archetype::{ArchetypeComponentId, Archetypes},
@ -168,6 +170,9 @@ mod tests {
#[derive(Component)]
struct W<T>(T);
#[derive(StageLabel)]
struct UpdateStage;
#[test]
fn simple_system() {
fn sys(query: Query<&A>) {
@ -188,7 +193,7 @@ mod tests {
let mut schedule = Schedule::default();
let mut update = SystemStage::parallel();
update.add_system(system);
schedule.add_stage("update", update);
schedule.add_stage(UpdateStage, update);
schedule.run(world);
}
@ -310,12 +315,15 @@ mod tests {
world.insert_resource(Added(0));
world.insert_resource(Changed(0));
#[derive(StageLabel)]
struct ClearTrackers;
let mut schedule = Schedule::default();
let mut update = SystemStage::parallel();
update.add_system(incr_e_on_flip);
schedule.add_stage("update", update);
schedule.add_stage(UpdateStage, update);
schedule.add_stage(
"clear_trackers",
ClearTrackers,
SystemStage::single(World::clear_trackers.exclusive_system()),
);

View File

@ -257,8 +257,10 @@ mod test {
world.insert_resource(Count(0));
let mut schedule = Schedule::default();
#[derive(StageLabel)]
struct Update;
schedule.add_stage(
"update",
Update,
SystemStage::parallel()
.with_run_criteria(FixedTimestep::step(0.5).with_label(LABEL))
.with_system(fixed_update),

View File

@ -105,6 +105,9 @@ mod test {
use crate::TransformBundle;
use bevy_hierarchy::{BuildChildren, BuildWorldChildren, Children, Parent};
#[derive(StageLabel)]
struct Update;
#[test]
fn did_propagate() {
let mut world = World::default();
@ -113,7 +116,7 @@ mod test {
update_stage.add_system(transform_propagate_system);
let mut schedule = Schedule::default();
schedule.add_stage("update", update_stage);
schedule.add_stage(Update, update_stage);
// Root entity
world
@ -156,7 +159,7 @@ mod test {
update_stage.add_system(transform_propagate_system);
let mut schedule = Schedule::default();
schedule.add_stage("update", update_stage);
schedule.add_stage(Update, update_stage);
// Root entity
let mut queue = CommandQueue::default();
@ -198,7 +201,7 @@ mod test {
update_stage.add_system(transform_propagate_system);
let mut schedule = Schedule::default();
schedule.add_stage("update", update_stage);
schedule.add_stage(Update, update_stage);
// Add parent entities
let mut children = Vec::new();

View File

@ -124,7 +124,7 @@ fn update_clipping(
mod tests {
use bevy_ecs::{
component::Component,
schedule::{Schedule, Stage, SystemStage},
schedule::{Schedule, Stage, StageLabel, SystemStage},
system::{CommandQueue, Commands},
world::World,
};
@ -150,6 +150,9 @@ mod tests {
(transform.translation.z / UI_Z_STEP).round() as u32
}
#[derive(StageLabel)]
struct Update;
#[test]
fn test_ui_z_system() {
let mut world = World::default();
@ -198,7 +201,7 @@ mod tests {
let mut schedule = Schedule::default();
let mut update_stage = SystemStage::parallel();
update_stage.add_system(ui_z_system);
schedule.add_stage("update", update_stage);
schedule.add_stage(Update, update_stage);
schedule.run(&mut world);
let mut actual_result = world

View File

@ -40,6 +40,9 @@ struct PostPhysics;
struct Done(bool);
fn main() {
#[derive(RunCriteriaLabel)]
struct IsDone;
App::new()
.add_plugins(DefaultPlugins)
.init_resource::<Done>()
@ -70,8 +73,7 @@ fn main() {
// This shows that we can modify existing run criteria results.
// Here we create a _not done_ criteria by piping the output of
// the `is_done` system and inverting the output.
// Notice a string literal also works as a label.
.with_run_criteria(RunCriteria::pipe("is_done_label", inverse))
.with_run_criteria(RunCriteria::pipe(IsDone, inverse))
// `collision` and `sfx` are not ordered with respect to
// each other, and may run in any order
.with_system(collision)
@ -80,7 +82,7 @@ fn main() {
.add_system(
exit.after(PostPhysics)
// Label the run criteria such that the `PostPhysics` set can reference it
.with_run_criteria(is_done.label("is_done_label")),
.with_run_criteria(is_done.label(IsDone)),
)
.run();
}