From 06f832adff201942f271aced1e6f3e4577120770 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Thu, 23 Dec 2021 23:41:25 +0000 Subject: [PATCH] make `sub_app` return an `&App` and add `sub_app_mut() -> &mut App` (#3309) It's sometimes useful to have a reference to an app a sub app at the same time, which is only possible with an immutable reference. --- crates/bevy_app/src/app.rs | 23 +++++++++++++++++++--- crates/bevy_core_pipeline/src/lib.rs | 2 +- crates/bevy_pbr/src/lib.rs | 2 +- crates/bevy_pbr/src/render/mesh.rs | 2 +- crates/bevy_pbr/src/wireframe.rs | 2 +- crates/bevy_render/src/camera/mod.rs | 2 +- crates/bevy_render/src/render_asset.rs | 2 +- crates/bevy_render/src/render_component.rs | 4 ++-- crates/bevy_render/src/texture/mod.rs | 2 +- crates/bevy_render/src/view/mod.rs | 2 +- crates/bevy_render/src/view/window.rs | 2 +- crates/bevy_sprite/src/lib.rs | 2 +- crates/bevy_text/src/lib.rs | 2 +- crates/bevy_ui/src/render/mod.rs | 2 +- examples/shader/shader_defs.rs | 2 +- examples/shader/shader_material.rs | 2 +- examples/window/multiple_windows.rs | 2 +- 17 files changed, 37 insertions(+), 20 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index a14baf2ae225f1..04024d794a4d32 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -854,8 +854,8 @@ impl App { } /// 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) { + pub fn sub_app_mut(&mut self, label: impl AppLabel) -> &mut App { + match self.get_sub_app_mut(label) { Ok(app) => app, Err(label) => panic!("Sub-App with label '{:?}' does not exist", label), } @@ -863,12 +863,29 @@ impl App { /// 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> { + pub fn get_sub_app_mut(&mut self, label: impl AppLabel) -> Result<&mut App, impl AppLabel> { self.sub_apps .get_mut((&label) as &dyn AppLabel) .map(|sub_app| &mut sub_app.app) .ok_or(label) } + + /// Retrieves a "sub app" stored inside this [App]. This will panic if the sub app does not exist. + pub fn sub_app(&self, label: impl AppLabel) -> &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(&self, label: impl AppLabel) -> Result<&App, impl AppLabel> { + self.sub_apps + .get((&label) as &dyn AppLabel) + .map(|sub_app| &sub_app.app) + .ok_or(label) + } } fn run_once(mut app: App) { diff --git a/crates/bevy_core_pipeline/src/lib.rs b/crates/bevy_core_pipeline/src/lib.rs index f55c7d2ef6b01e..709c4564879c44 100644 --- a/crates/bevy_core_pipeline/src/lib.rs +++ b/crates/bevy_core_pipeline/src/lib.rs @@ -88,7 +88,7 @@ impl Plugin for CorePipelinePlugin { fn build(&self, app: &mut App) { app.init_resource::(); - let render_app = app.sub_app(RenderApp); + let render_app = app.sub_app_mut(RenderApp); render_app .init_resource::>() .init_resource::>() diff --git a/crates/bevy_pbr/src/lib.rs b/crates/bevy_pbr/src/lib.rs index a95df2d4108a34..f2dc9e1ef868c3 100644 --- a/crates/bevy_pbr/src/lib.rs +++ b/crates/bevy_pbr/src/lib.rs @@ -135,7 +135,7 @@ impl Plugin for PbrPlugin { }, ); - let render_app = app.sub_app(RenderApp); + let render_app = app.sub_app_mut(RenderApp); render_app .add_system_to_stage( RenderStage::Extract, diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index d18cccd12073ee..eae586c32e394b 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -57,7 +57,7 @@ impl Plugin for MeshRenderPlugin { app.add_plugin(UniformComponentPlugin::::default()); - app.sub_app(RenderApp) + app.sub_app_mut(RenderApp) .init_resource::() .add_system_to_stage(RenderStage::Extract, extract_meshes) .add_system_to_stage(RenderStage::Queue, queue_mesh_bind_group) diff --git a/crates/bevy_pbr/src/wireframe.rs b/crates/bevy_pbr/src/wireframe.rs index 2069a54b16e445..c8d88b7d764e5d 100644 --- a/crates/bevy_pbr/src/wireframe.rs +++ b/crates/bevy_pbr/src/wireframe.rs @@ -31,7 +31,7 @@ impl Plugin for WireframePlugin { app.init_resource::(); - app.sub_app(RenderApp) + app.sub_app_mut(RenderApp) .add_render_command::() .init_resource::() .init_resource::>() diff --git a/crates/bevy_render/src/camera/mod.rs b/crates/bevy_render/src/camera/mod.rs index ef1e3d19aca12d..20baece4580e50 100644 --- a/crates/bevy_render/src/camera/mod.rs +++ b/crates/bevy_render/src/camera/mod.rs @@ -53,7 +53,7 @@ impl Plugin for CameraPlugin { CoreStage::PostUpdate, crate::camera::camera_system::, ); - app.sub_app(RenderApp) + app.sub_app_mut(RenderApp) .init_resource::() .add_system_to_stage(RenderStage::Extract, extract_cameras); } diff --git a/crates/bevy_render/src/render_asset.rs b/crates/bevy_render/src/render_asset.rs index 9d9cd099fd43ed..021a338a77f220 100644 --- a/crates/bevy_render/src/render_asset.rs +++ b/crates/bevy_render/src/render_asset.rs @@ -54,7 +54,7 @@ impl Default for RenderAssetPlugin { impl Plugin for RenderAssetPlugin { fn build(&self, app: &mut App) { - let render_app = app.sub_app(RenderApp); + let render_app = app.sub_app_mut(RenderApp); let prepare_asset_system = PrepareAssetSystem::::system(&mut render_app.world); render_app .init_resource::>() diff --git a/crates/bevy_render/src/render_component.rs b/crates/bevy_render/src/render_component.rs index 721e1b29182501..1296c921b14515 100644 --- a/crates/bevy_render/src/render_component.rs +++ b/crates/bevy_render/src/render_component.rs @@ -62,7 +62,7 @@ impl Default for UniformComponentPlugin { impl Plugin for UniformComponentPlugin { fn build(&self, app: &mut App) { - app.sub_app(RenderApp) + app.sub_app_mut(RenderApp) .insert_resource(ComponentUniforms::::default()) .add_system_to_stage( RenderStage::Prepare, @@ -144,7 +144,7 @@ where { fn build(&self, app: &mut App) { let system = ExtractComponentSystem::::system(&mut app.world); - let render_app = app.sub_app(RenderApp); + let render_app = app.sub_app_mut(RenderApp); render_app.add_system_to_stage(RenderStage::Extract, system); } } diff --git a/crates/bevy_render/src/texture/mod.rs b/crates/bevy_render/src/texture/mod.rs index 82ca2f41282882..cb586af04e5a59 100644 --- a/crates/bevy_render/src/texture/mod.rs +++ b/crates/bevy_render/src/texture/mod.rs @@ -35,7 +35,7 @@ impl Plugin for ImagePlugin { .unwrap() .set_untracked(DEFAULT_IMAGE_HANDLE, Image::default()); - app.sub_app(RenderApp) + app.sub_app_mut(RenderApp) .init_resource::() .add_system_to_stage(RenderStage::Cleanup, update_texture_cache_system); } diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index 710df62774f8a1..89dbfa53aa785c 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -26,7 +26,7 @@ impl Plugin for ViewPlugin { fn build(&self, app: &mut App) { app.init_resource::().add_plugin(VisibilityPlugin); - app.sub_app(RenderApp) + app.sub_app_mut(RenderApp) .init_resource::() .add_system_to_stage(RenderStage::Extract, extract_msaa) .add_system_to_stage(RenderStage::Prepare, prepare_view_uniforms) diff --git a/crates/bevy_render/src/view/window.rs b/crates/bevy_render/src/view/window.rs index 6bc9dafc2a7b4f..ac7ac18a2c733c 100644 --- a/crates/bevy_render/src/view/window.rs +++ b/crates/bevy_render/src/view/window.rs @@ -24,7 +24,7 @@ pub enum WindowSystem { impl Plugin for WindowRenderPlugin { fn build(&self, app: &mut App) { - app.sub_app(RenderApp) + app.sub_app_mut(RenderApp) .init_resource::() .init_resource::() .init_resource::() diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index c9ef2cf8f54fb1..3a6f266ee85e41 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -54,7 +54,7 @@ impl Plugin for SpritePlugin { let sprite_shader = Shader::from_wgsl(include_str!("render/sprite.wgsl")); shaders.set_untracked(SPRITE_SHADER_HANDLE, sprite_shader); app.add_asset::().register_type::(); - let render_app = app.sub_app(RenderApp); + let render_app = app.sub_app_mut(RenderApp); render_app .init_resource::() .init_resource::() diff --git a/crates/bevy_text/src/lib.rs b/crates/bevy_text/src/lib.rs index 0b81909b3cec1f..f245330e6821ae 100644 --- a/crates/bevy_text/src/lib.rs +++ b/crates/bevy_text/src/lib.rs @@ -49,7 +49,7 @@ impl Plugin for TextPlugin { .insert_resource(DefaultTextPipeline::default()) .add_system_to_stage(CoreStage::PostUpdate, text2d_system); - let render_app = app.sub_app(RenderApp); + let render_app = app.sub_app_mut(RenderApp); render_app.add_system_to_stage( RenderStage::Extract, extract_text2d_sprite.after(SpriteSystem::ExtractSprite), diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index e0b095416022b6..5750becce95c56 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -66,7 +66,7 @@ pub fn build_ui_render(app: &mut App) { let mut active_cameras = app.world.get_resource_mut::().unwrap(); active_cameras.add(CAMERA_UI); - let render_app = app.sub_app(RenderApp); + let render_app = app.sub_app_mut(RenderApp); render_app .init_resource::() .init_resource::>() diff --git a/examples/shader/shader_defs.rs b/examples/shader/shader_defs.rs index 18014e32192d9d..7cfd939f1ec2a4 100644 --- a/examples/shader/shader_defs.rs +++ b/examples/shader/shader_defs.rs @@ -23,7 +23,7 @@ pub struct IsRedPlugin; impl Plugin for IsRedPlugin { fn build(&self, app: &mut App) { app.add_plugin(ExtractComponentPlugin::::default()); - app.sub_app(RenderApp) + app.sub_app_mut(RenderApp) .add_render_command::() .init_resource::() .init_resource::>() diff --git a/examples/shader/shader_material.rs b/examples/shader/shader_material.rs index ad2f934f514afa..66af365ede362e 100644 --- a/examples/shader/shader_material.rs +++ b/examples/shader/shader_material.rs @@ -110,7 +110,7 @@ impl Plugin for CustomMaterialPlugin { app.add_asset::() .add_plugin(ExtractComponentPlugin::>::default()) .add_plugin(RenderAssetPlugin::::default()); - app.sub_app(RenderApp) + app.sub_app_mut(RenderApp) .add_render_command::() .init_resource::() .init_resource::>() diff --git a/examples/window/multiple_windows.rs b/examples/window/multiple_windows.rs index 3bfb07a958409d..6932460a209471 100644 --- a/examples/window/multiple_windows.rs +++ b/examples/window/multiple_windows.rs @@ -18,7 +18,7 @@ fn main() { .add_startup_system(setup) .add_startup_system(create_new_window); - let render_app = app.sub_app(RenderApp); + let render_app = app.sub_app_mut(RenderApp); render_app.add_system_to_stage(RenderStage::Extract, extract_secondary_camera_phases); let mut graph = render_app.world.get_resource_mut::().unwrap(); graph.add_node(SECONDARY_PASS_DRIVER, SecondaryCameraDriver);