Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Label type #1423

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2bd15fa
Add Label type
TheRawMeatball Feb 9, 2021
f41d7eb
General improvement
TheRawMeatball Feb 9, 2021
85eebf2
Fix stuff
TheRawMeatball Feb 9, 2021
857fa83
hotfix
TheRawMeatball Feb 9, 2021
d0cc270
rework to implement suggestions
TheRawMeatball Feb 15, 2021
52567f5
cargo fmt
TheRawMeatball Feb 15, 2021
37941ba
cargo test
TheRawMeatball Feb 15, 2021
fd3747c
Improve Debug impl on dyn Label
TheRawMeatball Feb 16, 2021
4b90fb0
Apply suggestions from code review
TheRawMeatball Feb 16, 2021
b8af092
hotfix
TheRawMeatball Feb 16, 2021
c28f3fd
Implement minor suggestion
TheRawMeatball Feb 16, 2021
099f8a6
Revert changes in tests
TheRawMeatball Feb 16, 2021
e70a8c1
Fix doctest
TheRawMeatball Feb 16, 2021
c002ba2
Fix inconsistencies
TheRawMeatball Feb 16, 2021
2e95129
Merge remote-tracking branch 'upstream/master' into zst-labels
TheRawMeatball Feb 16, 2021
c5724f6
Revert accidental changes
TheRawMeatball Feb 16, 2021
9c70a5d
Switch to requirig Debug impl
TheRawMeatball Feb 16, 2021
6d82c4e
cargo fmt
TheRawMeatball Feb 16, 2021
b7f1e85
Fix test
TheRawMeatball Feb 16, 2021
6e8f6fd
Merge 'upstream/master' into zst-labels
TheRawMeatball Feb 17, 2021
62f1eef
Fancify labels
TheRawMeatball Feb 17, 2021
da24443
mod/use consistency
cart Feb 17, 2021
7c20ed2
Implement requested changes
TheRawMeatball Feb 18, 2021
a904796
cargo fmt
TheRawMeatball Feb 18, 2021
ea539b0
Improvements from @Ratyzs
Ratysz Feb 18, 2021
3f0d074
Resolve conflicts
Ratysz Feb 18, 2021
c6db7b5
Explicit execution order ambiguities API (#1469) (#5)
Ratysz Feb 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 41 additions & 37 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::{
app::{App, AppExit},
event::Events,
plugin::Plugin,
stage, startup_stage, PluginGroup, PluginGroupBuilder,
CoreStage, PluginGroup, PluginGroupBuilder, StartupStage,
};
use bevy_ecs::{
clear_trackers_system, FromResources, IntoExclusiveSystem, IntoSystem, Resource, Resources,
RunOnce, Schedule, Stage, StateStage, SystemDescriptor, SystemStage, World,
RunOnce, Schedule, Stage, StageLabel, StateStage, SystemDescriptor, SystemStage, World,
};
use bevy_utils::tracing::debug;

Expand All @@ -24,7 +24,7 @@ impl Default for AppBuilder {
app_builder
.add_default_stages()
.add_event::<AppExit>()
.add_system_to_stage(stage::LAST, clear_trackers_system.exclusive_system());
.add_system_to_stage(CoreStage::LAST, clear_trackers_system.exclusive_system());
app_builder
}
}
Expand Down Expand Up @@ -54,15 +54,15 @@ impl AppBuilder {
self
}

pub fn add_stage<S: Stage>(&mut self, name: &'static str, stage: S) -> &mut Self {
pub fn add_stage<S: Stage>(&mut self, name: impl Into<StageLabel>, stage: S) -> &mut Self {
self.app.schedule.add_stage(name, stage);
self
}

pub fn add_stage_after<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
target: impl Into<StageLabel>,
name: impl Into<StageLabel>,
stage: S,
) -> &mut Self {
self.app.schedule.add_stage_after(target, name, stage);
Expand All @@ -71,93 +71,97 @@ impl AppBuilder {

pub fn add_stage_before<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
target: impl Into<StageLabel>,
name: impl Into<StageLabel>,
stage: S,
) -> &mut Self {
self.app.schedule.add_stage_before(target, name, stage);
self
}

pub fn add_startup_stage<S: Stage>(&mut self, name: &'static str, stage: S) -> &mut Self {
pub fn add_startup_stage<S: Stage>(
&mut self,
name: impl Into<StageLabel>,
stage: S,
) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
.stage(CoreStage::Startup, |schedule: &mut Schedule| {
schedule.add_stage(name, stage)
});
self
}

pub fn add_startup_stage_after<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
target: impl Into<StageLabel>,
name: impl Into<StageLabel>,
TheRawMeatball marked this conversation as resolved.
Show resolved Hide resolved
stage: S,
) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
.stage(CoreStage::Startup, |schedule: &mut Schedule| {
schedule.add_stage_after(target, name, stage)
});
self
}

pub fn add_startup_stage_before<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
target: impl Into<StageLabel>,
name: impl Into<StageLabel>,
stage: S,
) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
.stage(CoreStage::Startup, |schedule: &mut Schedule| {
schedule.add_stage_before(target, name, stage)
});
self
}

pub fn stage<T: Stage, F: FnOnce(&mut T) -> &mut T>(
&mut self,
name: &str,
name: impl Into<StageLabel>,
func: F,
) -> &mut Self {
self.app.schedule.stage(name, func);
self
}

pub fn add_system(&mut self, system: impl Into<SystemDescriptor>) -> &mut Self {
self.add_system_to_stage(stage::UPDATE, system)
self.add_system_to_stage(CoreStage::Update, system)
}

pub fn add_system_to_stage(
&mut self,
stage_name: &'static str,
stage_name: impl Into<StageLabel>,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
self.app.schedule.add_system_to_stage(stage_name, system);
self
}

pub fn add_startup_system(&mut self, system: impl Into<SystemDescriptor>) -> &mut Self {
self.add_startup_system_to_stage(startup_stage::STARTUP, system)
self.add_startup_system_to_stage(StartupStage::Startup, system)
}

pub fn add_startup_system_to_stage(
&mut self,
stage_name: &'static str,
stage_name: impl Into<StageLabel>,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
.stage(CoreStage::Startup, |schedule: &mut Schedule| {
schedule.add_system_to_stage(stage_name, system)
});
self
}

pub fn on_state_enter<T: Clone + Resource>(
&mut self,
stage: &str,
stage: impl Into<StageLabel>,
state: T,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
Expand All @@ -168,7 +172,7 @@ impl AppBuilder {

pub fn on_state_update<T: Clone + Resource>(
&mut self,
stage: &str,
stage: impl Into<StageLabel>,
state: T,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
Expand All @@ -179,7 +183,7 @@ impl AppBuilder {

pub fn on_state_exit<T: Clone + Resource>(
&mut self,
stage: &str,
stage: impl Into<StageLabel>,
state: T,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
Expand All @@ -190,28 +194,28 @@ impl AppBuilder {

pub fn add_default_stages(&mut self) -> &mut Self {
self.add_stage(
stage::STARTUP,
CoreStage::Startup,
Schedule::default()
.with_run_criteria(RunOnce::default())
.with_stage(startup_stage::PRE_STARTUP, SystemStage::parallel())
.with_stage(startup_stage::STARTUP, SystemStage::parallel())
.with_stage(startup_stage::POST_STARTUP, SystemStage::parallel()),
.with_stage(StartupStage::PreStartup, SystemStage::parallel())
.with_stage(StartupStage::Startup, SystemStage::parallel())
.with_stage(StartupStage::PostStartup, SystemStage::parallel()),
)
.add_stage(stage::FIRST, SystemStage::parallel())
.add_stage(stage::PRE_EVENT, SystemStage::parallel())
.add_stage(stage::EVENT, SystemStage::parallel())
.add_stage(stage::PRE_UPDATE, SystemStage::parallel())
.add_stage(stage::UPDATE, SystemStage::parallel())
.add_stage(stage::POST_UPDATE, SystemStage::parallel())
.add_stage(stage::LAST, SystemStage::parallel())
.add_stage(CoreStage::First, SystemStage::parallel())
.add_stage(CoreStage::PreEvent, SystemStage::parallel())
.add_stage(CoreStage::Event, SystemStage::parallel())
.add_stage(CoreStage::PreUpdate, SystemStage::parallel())
.add_stage(CoreStage::Update, SystemStage::parallel())
.add_stage(CoreStage::PostUpdate, SystemStage::parallel())
.add_stage(CoreStage::LAST, SystemStage::parallel())
}

pub fn add_event<T>(&mut self) -> &mut Self
where
T: Send + Sync + 'static,
{
self.insert_resource(Events::<T>::default())
.add_system_to_stage(stage::EVENT, Events::<T>::update_system.system())
.add_system_to_stage(CoreStage::Event, Events::<T>::update_system.system())
}

/// Inserts a resource to the current [App] and overwrites any resource previously added of the same type.
Expand Down
34 changes: 31 additions & 3 deletions crates/bevy_app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
use bevy_ecs::StageLabel;

/// The names of the default App stages
pub mod stage;
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
pub enum CoreStage {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO CoreStage and StartupStage should be marked #[non_exhaustive] to signal that they're not fixed or exhaustive, and avoid people trying to exhaustively pattern match to them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why'd they try to match this enum? Values of these types are only produced by user code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's a fair point. I'm trying to think of cases where you might possibly try but can't.

/// Name of the app stage that runs once at the beginning of the app
TheRawMeatball marked this conversation as resolved.
Show resolved Hide resolved
Startup,
/// Name of app stage that runs before all other app stages
First,
/// Name of app stage that runs before EVENT
PreEvent,
/// Name of app stage that updates events. Runs before UPDATE
Event,
/// Name of app stage responsible for performing setup before an update. Runs before UPDATE.
PreUpdate,
/// Name of app stage responsible for doing most app logic. Systems should be registered here by default.
Update,
/// Name of app stage responsible for processing the results of UPDATE. Runs after UPDATE.
PostUpdate,
/// Name of app stage that runs after all other app stages
LAST,
TheRawMeatball marked this conversation as resolved.
Show resolved Hide resolved
}
/// The names of the default App startup stages
pub mod startup_stage;
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
pub enum StartupStage {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to use the following order for imports:

  • mod declarations
  • public imports
  • private imports
  • code

/// Name of app stage that runs once before the startup stage
PreStartup,
/// Name of app stage that runs once when an app starts up
Startup,
/// Name of app stage that runs once after the startup stage
PostStartup,
}

mod app;
mod app_builder;
Expand All @@ -23,6 +51,6 @@ pub mod prelude {
app::App,
app_builder::AppBuilder,
event::{EventReader, Events},
stage, DynamicPlugin, Plugin, PluginGroup,
CoreStage, DynamicPlugin, Plugin, PluginGroup,
};
}
23 changes: 0 additions & 23 deletions crates/bevy_app/src/stage.rs

This file was deleted.

8 changes: 0 additions & 8 deletions crates/bevy_app/src/startup_stage.rs

This file was deleted.

4 changes: 2 additions & 2 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ impl AddAsset for AppBuilder {

self.insert_resource(assets)
.add_system_to_stage(
super::stage::ASSET_EVENTS,
super::AssetStage::AssetEvents,
Assets::<T>::asset_event_system.system(),
)
.add_system_to_stage(
crate::stage::LOAD_ASSETS,
crate::AssetStage::LoadAssets,
update_asset_storage_system::<T>.system(),
)
.register_type::<Handle<T>>()
Expand Down
24 changes: 14 additions & 10 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ pub use loader::*;
pub use path::*;

/// The names of asset stages in an App Schedule
pub mod stage {
pub const LOAD_ASSETS: &str = "load_assets";
pub const ASSET_EVENTS: &str = "asset_events";
use bevy_ecs::StageLabel;
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
pub enum AssetStage {
LoadAssets,
AssetEvents,
}

pub mod prelude {
pub use crate::{AddAsset, AssetEvent, AssetServer, Assets, Handle, HandleUntyped};
}
Expand Down Expand Up @@ -89,25 +90,28 @@ impl Plugin for AssetPlugin {
}

app.add_stage_before(
bevy_app::stage::PRE_UPDATE,
stage::LOAD_ASSETS,
bevy_app::CoreStage::PreUpdate,
AssetStage::LoadAssets,
SystemStage::parallel(),
)
.add_stage_after(
bevy_app::stage::POST_UPDATE,
stage::ASSET_EVENTS,
bevy_app::CoreStage::PostUpdate,
AssetStage::AssetEvents,
SystemStage::parallel(),
)
.register_type::<HandleId>()
.add_system_to_stage(
bevy_app::stage::PRE_UPDATE,
bevy_app::CoreStage::PreUpdate,
asset_server::free_unused_assets_system.system(),
);

#[cfg(all(
feature = "filesystem_watcher",
all(not(target_arch = "wasm32"), not(target_os = "android"))
))]
app.add_system_to_stage(stage::LOAD_ASSETS, io::filesystem_watcher_system.system());
app.add_system_to_stage(
AssetStage::LoadAssets,
io::filesystem_watcher_system.system(),
);
}
}
2 changes: 1 addition & 1 deletion crates/bevy_audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Plugin for AudioPlugin {
.init_asset_loader::<Mp3Loader>()
.init_resource::<Audio<AudioSource>>()
.add_system_to_stage(
stage::POST_UPDATE,
CoreStage::PostUpdate,
play_queued_audio_system::<AudioSource>.exclusive_system(),
);
}
Expand Down
9 changes: 6 additions & 3 deletions crates/bevy_core/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ pub(crate) fn entity_labels_system(
#[cfg(test)]
mod tests {
use super::*;
use bevy_ecs::Stage;
use bevy_ecs::{Stage, StageLabel};

#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
struct Test;

fn setup() -> (World, Resources, bevy_ecs::Schedule) {
let world = World::new();
let mut resources = Resources::default();
resources.insert(EntityLabels::default());
let mut schedule = bevy_ecs::Schedule::default();
schedule.add_stage("test", SystemStage::single_threaded());
schedule.add_system_to_stage("test", entity_labels_system.system());
schedule.add_stage(Test, SystemStage::single_threaded());
schedule.add_system_to_stage(Test, entity_labels_system.system());
(world, resources, schedule)
}

Expand Down
Loading