From 00c6ac4e6890233064f62ac5f9049612c4775a16 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Sun, 12 Dec 2021 14:54:45 +0100 Subject: [PATCH] add methods to get sub app by non-mut reference --- crates/bevy_app/src/app.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index a14baf2ae225f..04024d794a4d3 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) {