Skip to content

Commit 958a898

Browse files
committed
Remove App::add_sub_app (#7290)
# Objective Fixes #7286. Both `App::add_sub_app` and `App::insert_sub_app` are rather redundant. Before 0.10 is shipped, one of them should be removed. ## Solution Remove `App::add_sub_app` to prefer `App::insert_sub_app`. Also hid away `SubApp::extract` since that can be a footgun if someone mutates it for whatever reason. Willing to revert this change if there are objections. Perhaps we should make `SubApp: Deref<Target=App>`? Might change if we decide to move `!Send` resources into it. --- ## Changelog Added: `SubApp::new` Removed: `App::add_sub_app` ## Migration Guide `App::add_sub_app` has been removed in favor of `App::insert_sub_app`. Use `SubApp::new` and insert it via `App::add_sub_app` Old: ```rust let mut sub_app = App::new() // Build subapp here app.add_sub_app(MySubAppLabel, sub_app); ``` New: ```rust let mut sub_app = App::new() // Build subapp here app.insert_sub_app(MySubAppLabel, SubApp::new(sub_app, extract_fn)); ```
1 parent 3c63c0d commit 958a898

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
lines changed

crates/bevy_app/src/app.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub struct App {
6161
/// The main ECS [`World`] of the [`App`].
6262
/// This stores and provides access to all the main data of the application.
6363
/// The systems of the [`App`] will run using this [`World`].
64-
/// If additional separate [`World`]-[`Schedule`] pairs are needed, you can use [`sub_app`](App::add_sub_app)s.
64+
/// If additional separate [`World`]-[`Schedule`] pairs are needed, you can use [`sub_app`](App::insert_sub_app)s.
6565
pub world: World,
6666
/// The [runner function](Self::set_runner) is primarily responsible for managing
6767
/// the application's event loop and advancing the [`Schedule`].
@@ -94,7 +94,7 @@ impl Debug for App {
9494
/// # Example
9595
///
9696
/// ```rust
97-
/// # use bevy_app::{App, AppLabel};
97+
/// # use bevy_app::{App, AppLabel, SubApp};
9898
/// # use bevy_ecs::prelude::*;
9999
///
100100
/// #[derive(Resource, Default)]
@@ -122,9 +122,9 @@ impl Debug for App {
122122
/// sub_app.add_stage(ExampleStage, example_stage);
123123
///
124124
/// // add the sub_app to the app
125-
/// app.add_sub_app(ExampleApp, sub_app, |main_world, sub_app| {
125+
/// app.insert_sub_app(ExampleApp, SubApp::new(sub_app, |main_world, sub_app| {
126126
/// sub_app.world.resource_mut::<Val>().0 = main_world.resource::<Val>().0;
127-
/// });
127+
/// }));
128128
///
129129
/// // This will run the schedules once, since we're using the default runner
130130
/// app.run();
@@ -135,10 +135,23 @@ pub struct SubApp {
135135

136136
/// A function that allows access to both the [`SubApp`] [`World`] and the main [`App`]. This is
137137
/// useful for moving data between the sub app and the main app.
138-
pub extract: Box<dyn Fn(&mut World, &mut App) + Send>,
138+
extract: Box<dyn Fn(&mut World, &mut App) + Send>,
139139
}
140140

141141
impl SubApp {
142+
/// Creates a new [`SubApp`].
143+
///
144+
/// The provided function `extract` is normally called by the [`update`](App::update) method.
145+
/// After extract is called, the [`Schedule`] of the sub app is run. The [`World`]
146+
/// parameter represents the main app world, while the [`App`] parameter is just a mutable
147+
/// reference to the `SubApp` itself.
148+
pub fn new(app: App, extract: impl Fn(&mut World, &mut App) + Send + 'static) -> Self {
149+
Self {
150+
app,
151+
extract: Box::new(extract),
152+
}
153+
}
154+
142155
/// Runs the `SubApp`'s schedule.
143156
pub fn run(&mut self) {
144157
self.app.schedule.run(&mut self.app.world);
@@ -204,7 +217,7 @@ impl App {
204217
///
205218
/// This method also updates sub apps.
206219
///
207-
/// See [`add_sub_app`](Self::add_sub_app) and [`run_once`](Schedule::run_once) for more details.
220+
/// See [`insert_sub_app`](Self::insert_sub_app) and [`run_once`](Schedule::run_once) for more details.
208221
pub fn update(&mut self) {
209222
{
210223
#[cfg(feature = "trace")]
@@ -1081,28 +1094,6 @@ impl App {
10811094
self
10821095
}
10831096

1084-
/// Adds an [`App`] as a child of the current one.
1085-
///
1086-
/// The provided function `extract` is normally called by the [`update`](Self::update) method.
1087-
/// After extract is called, the [`Schedule`] of the sub app is run. The [`World`]
1088-
/// parameter represents the main app world, while the [`App`] parameter is just a mutable
1089-
/// reference to the `SubApp` itself.
1090-
pub fn add_sub_app(
1091-
&mut self,
1092-
label: impl AppLabel,
1093-
app: App,
1094-
extract: impl Fn(&mut World, &mut App) + 'static + Send,
1095-
) -> &mut Self {
1096-
self.sub_apps.insert(
1097-
label.as_label(),
1098-
SubApp {
1099-
app,
1100-
extract: Box::new(extract),
1101-
},
1102-
);
1103-
self
1104-
}
1105-
11061097
/// Retrieves a `SubApp` stored inside this [`App`].
11071098
///
11081099
/// # Panics

crates/bevy_render/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use crate::{
5050
settings::WgpuSettings,
5151
view::{ViewPlugin, WindowRenderPlugin},
5252
};
53-
use bevy_app::{App, AppLabel, Plugin};
53+
use bevy_app::{App, AppLabel, Plugin, SubApp};
5454
use bevy_asset::{AddAsset, AssetServer};
5555
use bevy_ecs::{prelude::*, system::SystemState};
5656
use bevy_utils::tracing::debug;
@@ -231,7 +231,7 @@ impl Plugin for RenderPlugin {
231231
app.insert_resource(receiver);
232232
render_app.insert_resource(sender);
233233

234-
app.add_sub_app(RenderApp, render_app, move |app_world, render_app| {
234+
app.insert_sub_app(RenderApp, SubApp::new(render_app, move |app_world, render_app| {
235235
#[cfg(feature = "trace")]
236236
let _render_span = bevy_utils::tracing::info_span!("extract main app to render subapp").entered();
237237
{
@@ -267,7 +267,7 @@ impl Plugin for RenderPlugin {
267267
// extract
268268
extract(app_world, render_app);
269269
}
270-
});
270+
}));
271271
}
272272

273273
app.add_plugin(ValidParentCheckPlugin::<view::ComputedVisibility>::default())

crates/bevy_render/src/pipelined_rendering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Plugin for PipelinedRenderingPlugin {
8282
RenderExtractStage::BeforeIoAfterRenderStart,
8383
SystemStage::parallel(),
8484
);
85-
app.add_sub_app(RenderExtractApp, sub_app, update_rendering);
85+
app.insert_sub_app(RenderExtractApp, SubApp::new(sub_app, update_rendering));
8686
}
8787

8888
// Sets up the render thread and inserts resources into the main app used for controlling the render thread.

0 commit comments

Comments
 (0)