diff --git a/crates/bevy_app/Cargo.toml b/crates/bevy_app/Cargo.toml index f73f333392e1e..58e8ac243e79c 100644 --- a/crates/bevy_app/Cargo.toml +++ b/crates/bevy_app/Cargo.toml @@ -23,7 +23,6 @@ bevy_derive = { path = "../bevy_derive", version = "0.5.0" } bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" } bevy_reflect = { path = "../bevy_reflect", version = "0.5.0", optional = true } bevy_utils = { path = "../bevy_utils", version = "0.5.0" } -bevy_app_macros = { path = "./macros", version = "0.5.0" } # other serde = { version = "1.0", features = ["derive"], optional = true } diff --git a/crates/bevy_app/macros/Cargo.toml b/crates/bevy_app/macros/Cargo.toml deleted file mode 100644 index 505c27068b690..0000000000000 --- a/crates/bevy_app/macros/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "bevy_app_macros" -version = "0.5.0" -description = "Bevy App Macros" -edition = "2018" -license = "MIT OR Apache-2.0" - -[lib] -proc-macro = true - -[dependencies] -bevy_macro_utils = { path = "../../bevy_macro_utils", version = "0.5.0" } - -syn = "1.0" -quote = "1.0" -proc-macro2 = "1.0" diff --git a/crates/bevy_app/macros/src/lib.rs b/crates/bevy_app/macros/src/lib.rs deleted file mode 100644 index eafa3dde55cda..0000000000000 --- a/crates/bevy_app/macros/src/lib.rs +++ /dev/null @@ -1,19 +0,0 @@ -extern crate proc_macro; - -use bevy_macro_utils::{derive_label, BevyManifest}; -use proc_macro::TokenStream; -use quote::format_ident; - -#[proc_macro_derive(SubAppLabel)] -pub fn derive_sub_app_label(input: TokenStream) -> TokenStream { - let input = syn::parse_macro_input!(input as syn::DeriveInput); - let mut trait_path = bevy_app_path(); - trait_path - .segments - .push(format_ident!("SubAppLabel").into()); - derive_label(input, trait_path) -} - -fn bevy_app_path() -> syn::Path { - BevyManifest::default().get_path("bevy_app") -} diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index faf7a4cc1590c..c7f28970a5e98 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -1,3 +1,5 @@ +pub use bevy_derive::AppLabel; + use crate::{CoreStage, Events, Plugin, PluginGroup, PluginGroupBuilder, StartupStage}; use bevy_ecs::{ component::{Component, ComponentDescriptor}, @@ -10,13 +12,10 @@ use bevy_ecs::{ use bevy_utils::{tracing::debug, HashMap}; use std::{fmt::Debug, hash::Hash}; -pub use bevy_app_macros::SubAppLabel; - #[cfg(feature = "trace")] use bevy_utils::tracing::info_span; -bevy_utils::define_label!(SubAppLabel); -type BoxedSubAppLabel = Box; +bevy_utils::define_label!(AppLabel); #[allow(clippy::needless_doctest_main)] /// Containers of app logic and data @@ -45,7 +44,7 @@ pub struct App { pub world: World, pub runner: Box, pub schedule: Schedule, - sub_apps: HashMap, + sub_apps: HashMap, SubApp>, } struct SubApp { @@ -594,7 +593,7 @@ impl App { pub fn add_sub_app( &mut self, - label: impl SubAppLabel, + label: impl AppLabel, app: App, f: impl Fn(&mut World, &mut App) + 'static, ) -> &mut Self { @@ -608,11 +607,21 @@ impl App { self } - pub fn sub_app_mut(&mut self, label: impl SubAppLabel) -> Option<&mut App> { - let label = Box::new(label) as BoxedSubAppLabel; + /// Retrieves a "sub app" stored inside this [App]. This will panic if the sub app does not exist. + pub fn sub_app(&mut self, label: impl AppLabel) -> &mut App { + match self.get_sub_app(label) { + Ok(app) => app, + Err(label) => panic!("Sub-App with label '{:?}' does not exist", label), + } + } + + /// Retrieves a "sub app" inside this [App] with the given label, if it exists. Otherwise returns + /// an [Err] containing the given label. + pub fn get_sub_app(&mut self, label: impl AppLabel) -> Result<&mut App, impl AppLabel> { self.sub_apps - .get_mut(&label) + .get_mut((&label) as &dyn AppLabel) .map(|sub_app| &mut sub_app.app) + .ok_or(label) } } diff --git a/crates/bevy_derive/src/lib.rs b/crates/bevy_derive/src/lib.rs index a73093ed2aa7e..7f21428fc2a7a 100644 --- a/crates/bevy_derive/src/lib.rs +++ b/crates/bevy_derive/src/lib.rs @@ -10,7 +10,9 @@ mod render_resources; mod resource; mod shader_defs; +use bevy_macro_utils::{derive_label, BevyManifest}; use proc_macro::TokenStream; +use quote::format_ident; /// Derives the FromResources trait. Each field must also implement the FromResources trait or this /// will fail. FromResources is automatically implemented for types that implement Default. @@ -60,3 +62,11 @@ pub fn bevy_main(attr: TokenStream, item: TokenStream) -> TokenStream { pub fn derive_enum_variant_meta(input: TokenStream) -> TokenStream { enum_variant_meta::derive_enum_variant_meta(input) } + +#[proc_macro_derive(AppLabel)] +pub fn derive_app_label(input: TokenStream) -> TokenStream { + let input = syn::parse_macro_input!(input as syn::DeriveInput); + let mut trait_path = BevyManifest::default().get_path("bevy_app"); + trait_path.segments.push(format_ident!("AppLabel").into()); + derive_label(input, trait_path) +} diff --git a/pipelined/bevy_core_pipeline/src/lib.rs b/pipelined/bevy_core_pipeline/src/lib.rs index 6f7d447ac5fb7..c9187adf39daa 100644 --- a/pipelined/bevy_core_pipeline/src/lib.rs +++ b/pipelined/bevy_core_pipeline/src/lib.rs @@ -20,7 +20,7 @@ use bevy_render2::{ renderer::RenderDevice, texture::TextureCache, view::{ExtractedView, ViewPlugin}, - RenderStage, RenderSubApp, RenderWorld, + RenderApp, RenderStage, RenderWorld, }; /// Resource that configures the clear color @@ -74,7 +74,7 @@ impl Plugin for CorePipelinePlugin { fn build(&self, app: &mut App) { app.init_resource::(); - let render_app = app.sub_app_mut(RenderSubApp).unwrap(); + let render_app = app.sub_app(RenderApp); render_app .add_system_to_stage(RenderStage::Extract, extract_clear_color) .add_system_to_stage(RenderStage::Extract, extract_core_pipeline_camera_phases) diff --git a/pipelined/bevy_pbr2/src/lib.rs b/pipelined/bevy_pbr2/src/lib.rs index 4490e660b6514..8e1a0dc8f5b48 100644 --- a/pipelined/bevy_pbr2/src/lib.rs +++ b/pipelined/bevy_pbr2/src/lib.rs @@ -13,7 +13,7 @@ use bevy_ecs::prelude::*; use bevy_render2::{ render_graph::RenderGraph, render_phase::{sort_phase_system, DrawFunctions}, - RenderStage, RenderSubApp, + RenderApp, RenderStage, }; pub mod draw_3d_graph { @@ -30,7 +30,7 @@ impl Plugin for PbrPlugin { app.add_plugin(StandardMaterialPlugin) .init_resource::(); - let render_app = app.sub_app_mut(RenderSubApp).unwrap(); + let render_app = app.sub_app(RenderApp); render_app .add_system_to_stage(RenderStage::Extract, render::extract_meshes) .add_system_to_stage(RenderStage::Extract, render::extract_lights) diff --git a/pipelined/bevy_render2/src/camera/mod.rs b/pipelined/bevy_render2/src/camera/mod.rs index cade9c514962b..597e54c98c26d 100644 --- a/pipelined/bevy_render2/src/camera/mod.rs +++ b/pipelined/bevy_render2/src/camera/mod.rs @@ -12,7 +12,7 @@ pub use bundle::*; pub use camera::*; pub use projection::*; -use crate::{view::ExtractedView, RenderStage, RenderSubApp}; +use crate::{view::ExtractedView, RenderApp, RenderStage}; use bevy_app::{App, CoreStage, Plugin}; use bevy_ecs::prelude::*; @@ -40,8 +40,7 @@ impl Plugin for CameraPlugin { CoreStage::PostUpdate, crate::camera::camera_system::, ); - let render_app = app.sub_app_mut(RenderSubApp).unwrap(); - render_app + app.sub_app(RenderApp) .init_resource::() .add_system_to_stage(RenderStage::Extract, extract_cameras); } diff --git a/pipelined/bevy_render2/src/lib.rs b/pipelined/bevy_render2/src/lib.rs index 49d73e97e43a8..b4f8467fce75c 100644 --- a/pipelined/bevy_render2/src/lib.rs +++ b/pipelined/bevy_render2/src/lib.rs @@ -24,7 +24,7 @@ use crate::{ texture::ImagePlugin, view::{ViewPlugin, WindowRenderPlugin}, }; -use bevy_app::{App, Plugin, SubAppLabel}; +use bevy_app::{App, AppLabel, Plugin}; use bevy_ecs::prelude::*; #[derive(Default)] @@ -74,8 +74,8 @@ impl DerefMut for RenderWorld { } /// Label for the rendering sub-app -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, SubAppLabel)] -pub struct RenderSubApp; +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, AppLabel)] +pub struct RenderApp; /// A "scratch" world used to avoid allocating new worlds every frame when // swapping out the Render World. @@ -118,7 +118,7 @@ impl Plugin for RenderPlugin { .init_resource::() .init_resource::(); - app.add_sub_app(RenderSubApp, render_app, move |app_world, render_app| { + app.add_sub_app(RenderApp, render_app, move |app_world, render_app| { // reserve all existing app entities for use in render_app // they can only be spawned using `get_or_spawn()` let meta_len = app_world.entities().meta.len(); diff --git a/pipelined/bevy_render2/src/render_asset.rs b/pipelined/bevy_render2/src/render_asset.rs index 66fd9f2dabdbc..4185c93cc09c2 100644 --- a/pipelined/bevy_render2/src/render_asset.rs +++ b/pipelined/bevy_render2/src/render_asset.rs @@ -2,7 +2,7 @@ use std::marker::PhantomData; use crate::{ renderer::{RenderDevice, RenderQueue}, - RenderStage, RenderSubApp, + RenderApp, RenderStage, }; use bevy_app::{App, Plugin}; use bevy_asset::{Asset, AssetEvent, Assets, Handle}; @@ -31,8 +31,7 @@ impl Default for RenderAssetPlugin { impl Plugin for RenderAssetPlugin { fn build(&self, app: &mut App) { - let render_app = app.sub_app_mut(RenderSubApp).unwrap(); - render_app + app.sub_app(RenderApp) .init_resource::>() .init_resource::>() .add_system_to_stage(RenderStage::Extract, extract_render_asset::) diff --git a/pipelined/bevy_render2/src/texture/mod.rs b/pipelined/bevy_render2/src/texture/mod.rs index efe05dc5616f4..cd25dd57387ae 100644 --- a/pipelined/bevy_render2/src/texture/mod.rs +++ b/pipelined/bevy_render2/src/texture/mod.rs @@ -13,7 +13,7 @@ pub use hdr_texture_loader::*; pub use image_texture_loader::*; pub use texture_cache::*; -use crate::{render_asset::RenderAssetPlugin, RenderStage, RenderSubApp}; +use crate::{render_asset::RenderAssetPlugin, RenderApp, RenderStage}; use bevy_app::{App, Plugin}; use bevy_asset::AddAsset; @@ -30,8 +30,7 @@ impl Plugin for ImagePlugin { app.add_plugin(RenderAssetPlugin::::default()) .add_asset::(); - let render_app = app.sub_app_mut(RenderSubApp).unwrap(); - render_app + app.sub_app(RenderApp) .init_resource::() .add_system_to_stage(RenderStage::Cleanup, update_texture_cache_system); } diff --git a/pipelined/bevy_render2/src/view/mod.rs b/pipelined/bevy_render2/src/view/mod.rs index 911148116b3c7..e00a3bdcdc5bc 100644 --- a/pipelined/bevy_render2/src/view/mod.rs +++ b/pipelined/bevy_render2/src/view/mod.rs @@ -7,7 +7,7 @@ use crate::{ render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext}, render_resource::DynamicUniformVec, renderer::{RenderContext, RenderDevice}, - RenderStage, RenderSubApp, + RenderApp, RenderStage, }; use bevy_app::{App, Plugin}; use bevy_ecs::prelude::*; @@ -22,7 +22,7 @@ impl ViewPlugin { impl Plugin for ViewPlugin { fn build(&self, app: &mut App) { - let render_app = app.sub_app_mut(RenderSubApp).unwrap(); + let render_app = app.sub_app(RenderApp); render_app .init_resource::() .add_system_to_stage(RenderStage::Prepare, prepare_views); diff --git a/pipelined/bevy_render2/src/view/window.rs b/pipelined/bevy_render2/src/view/window.rs index dbcf46f4c3010..cb71008f572b3 100644 --- a/pipelined/bevy_render2/src/view/window.rs +++ b/pipelined/bevy_render2/src/view/window.rs @@ -2,7 +2,7 @@ use crate::{ render_resource::TextureView, renderer::{RenderDevice, RenderInstance}, texture::BevyDefault, - RenderStage, RenderSubApp, + RenderApp, RenderStage, }; use bevy_app::{App, Plugin}; use bevy_ecs::prelude::*; @@ -19,8 +19,7 @@ pub struct WindowRenderPlugin; impl Plugin for WindowRenderPlugin { fn build(&self, app: &mut App) { - let render_app = app.sub_app_mut(RenderSubApp).unwrap(); - render_app + app.sub_app(RenderApp) .init_resource::() .init_resource::() .add_system_to_stage(RenderStage::Extract, extract_windows) diff --git a/pipelined/bevy_sprite2/src/lib.rs b/pipelined/bevy_sprite2/src/lib.rs index a1a7643ea69d3..5c77bf0714765 100644 --- a/pipelined/bevy_sprite2/src/lib.rs +++ b/pipelined/bevy_sprite2/src/lib.rs @@ -17,7 +17,7 @@ pub use texture_atlas_builder::*; use bevy_app::prelude::*; use bevy_render2::{ - render_graph::RenderGraph, render_phase::DrawFunctions, RenderStage, RenderSubApp, + render_graph::RenderGraph, render_phase::DrawFunctions, RenderApp, RenderStage, }; #[derive(Default)] @@ -28,7 +28,7 @@ impl Plugin for SpritePlugin { app.add_asset::() .register_type::() .add_system_to_stage(CoreStage::PostUpdate, sprite_auto_resize_system); - let render_app = app.sub_app_mut(RenderSubApp).unwrap(); + let render_app = app.sub_app(RenderApp); render_app .init_resource::() .add_system_to_stage(RenderStage::Extract, render::extract_atlases) @@ -44,8 +44,7 @@ impl Plugin for SpritePlugin { .unwrap() .write() .add(draw_sprite); - let render_world = app.sub_app_mut(RenderSubApp).unwrap().world.cell(); - let mut graph = render_world.get_resource_mut::().unwrap(); + let mut graph = render_app.world.get_resource_mut::().unwrap(); graph.add_node("sprite", SpriteNode); graph .add_node_edge("sprite", bevy_core_pipeline::node::MAIN_PASS_DEPENDENCIES) diff --git a/tools/publish.sh b/tools/publish.sh index 62b7181c9014a..ca35a3e327c09 100644 --- a/tools/publish.sh +++ b/tools/publish.sh @@ -7,7 +7,6 @@ crates=( bevy_tasks bevy_ecs/macros bevy_ecs - bevy_app/macros bevy_app bevy_log bevy_dynamic_plugin