From 29c9ea35acd7df112acf0e442596a6081d90ef0c Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Sat, 26 Feb 2022 13:16:55 -0500 Subject: [PATCH 01/17] Updated docs --- crates/bevy_ecs/src/world/mod.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index b130d7a54736a..279c6771234fb 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -705,13 +705,6 @@ impl World { self.get_populated_resource_column(component_id).is_some() } - /// Gets a reference to the resource of the given type, if it exists. Otherwise returns [None] - #[inline] - pub fn get_resource(&self) -> Option<&R> { - let component_id = self.components.get_resource_id(TypeId::of::())?; - unsafe { self.get_resource_with_id(component_id) } - } - pub fn is_resource_added(&self) -> bool { let component_id = if let Some(component_id) = self.components.get_resource_id(TypeId::of::()) { @@ -746,7 +739,28 @@ impl World { ticks.is_changed(self.last_change_tick(), self.read_change_tick()) } - /// Gets a mutable reference to the resource of the given type, if it exists. Otherwise returns + /// Gets a reference to the resource of the given type + /// + /// # Panics + /// Panics if the resource does not exist. + /// Use [`try_get_resource_mut`](World::try_get_resource) instead if you want to handle this case. + /// + /// If you want to instead insert a value if the resource does not exist, + /// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with). + #[inline] + pub fn get_resource(&self) -> Option<&R> { + let component_id = self.components.get_resource_id(TypeId::of::())?; + unsafe { self.get_resource_with_id(component_id) } + } + + /// Gets a mutable reference to the resource of the given type + /// + /// # Panics + /// Panics if the resource does not exist. + /// Use [`try_get_resource_mut`](World::try_get_resource_mut) instead if you want to handle this case. + /// + /// If you want to instead insert a value if the resource does not exist, + /// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with). #[inline] pub fn get_resource_mut(&mut self) -> Option> { // SAFE: unique world access @@ -754,7 +768,7 @@ impl World { } // PERF: optimize this to avoid redundant lookups - /// Gets a resource of type `T` if it exists, + /// Gets a mutable reference to the resource of type `T` if it exists, /// otherwise inserts the resource using the result of calling `func`. #[inline] pub fn get_resource_or_insert_with( From 96463a6cf7b41205b3f951cec9ef393769ac00ec Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Sat, 26 Feb 2022 13:19:16 -0500 Subject: [PATCH 02/17] Try get resource methods --- crates/bevy_ecs/src/world/mod.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 279c6771234fb..a02d4a27fef9b 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -748,9 +748,9 @@ impl World { /// If you want to instead insert a value if the resource does not exist, /// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with). #[inline] - pub fn get_resource(&self) -> Option<&R> { - let component_id = self.components.get_resource_id(TypeId::of::())?; - unsafe { self.get_resource_with_id(component_id) } + pub fn get_resource(&self) -> &R { + self.try_get_resource() + .expect("Could not find resource in `World`. Did you forget to add it?") } /// Gets a mutable reference to the resource of the given type @@ -762,7 +762,22 @@ impl World { /// If you want to instead insert a value if the resource does not exist, /// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with). #[inline] - pub fn get_resource_mut(&mut self) -> Option> { + pub fn get_resource_mut(&mut self) -> Mut<'_, R> { + // SAFE: unique world access + self.try_get_resource_mut() + .expect("Could not find resource in `World`. Did you forget to add it?") + } + + /// Gets a reference to the resource of the given type if it exists + #[inline] + pub fn try_get_resource(&self) -> Option<&R> { + let component_id = self.components.get_resource_id(TypeId::of::())?; + unsafe { self.get_resource_with_id(component_id) } + } + + /// Gets a mutable reference to the resource of the given type if it exists + #[inline] + pub fn try_get_resource_mut(&mut self) -> Option> { // SAFE: unique world access unsafe { self.get_resource_unchecked_mut() } } From c8294af82d7c4f60fe5986b536a00ec62dc32c09 Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Sat, 26 Feb 2022 13:34:50 -0500 Subject: [PATCH 03/17] Update usages of .get_resource --- crates/bevy_app/src/app.rs | 3 +- crates/bevy_app/src/schedule_runner.rs | 4 +- crates/bevy_asset/src/asset_server.rs | 8 +- crates/bevy_asset/src/assets.rs | 17 +-- crates/bevy_asset/src/lib.rs | 9 +- crates/bevy_core/src/lib.rs | 2 +- crates/bevy_core/src/time/fixed_timestep.rs | 12 +- .../src/entity_count_diagnostics_plugin.rs | 2 +- crates/bevy_ecs/src/lib.rs | 25 ++-- .../src/schedule/executor_parallel.rs | 6 +- crates/bevy_ecs/src/schedule/stage.rs | 120 +++++++----------- crates/bevy_ecs/src/schedule/state.rs | 18 +-- .../bevy_ecs/src/system/exclusive_system.rs | 6 +- crates/bevy_ecs/src/system/mod.rs | 30 ++--- crates/bevy_ecs/src/world/mod.rs | 2 +- crates/bevy_render/src/lib.rs | 6 +- crates/bevy_render/src/render_phase/draw.rs | 2 +- .../src/render_resource/pipeline_cache.rs | 2 +- crates/bevy_render/src/renderer/mod.rs | 8 +- crates/bevy_render/src/texture/mod.rs | 1 - crates/bevy_render/src/view/window.rs | 2 +- crates/bevy_scene/src/command.rs | 4 +- crates/bevy_scene/src/dynamic_scene.rs | 2 +- crates/bevy_scene/src/scene_loader.rs | 2 +- crates/bevy_scene/src/scene_spawner.rs | 6 +- crates/bevy_window/src/lib.rs | 7 +- crates/bevy_winit/src/lib.rs | 7 +- 27 files changed, 125 insertions(+), 188 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 5bbb9b5676abd..18bab8b3110e3 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -841,8 +841,7 @@ impl App { { let registry = self .world - .get_resource_mut::() - .unwrap(); + .get_resource_mut::(); registry.write().register::(); } self diff --git a/crates/bevy_app/src/schedule_runner.rs b/crates/bevy_app/src/schedule_runner.rs index 3227826f70d06..9611dd3e47b89 100644 --- a/crates/bevy_app/src/schedule_runner.rs +++ b/crates/bevy_app/src/schedule_runner.rs @@ -79,7 +79,7 @@ impl Plugin for ScheduleRunnerPlugin { let start_time = Instant::now(); if let Some(app_exit_events) = - app.world.get_resource_mut::>() + app.world.try_get_resource_mut::>() { if let Some(exit) = app_exit_event_reader.iter(&app_exit_events).last() { @@ -90,7 +90,7 @@ impl Plugin for ScheduleRunnerPlugin { app.update(); if let Some(app_exit_events) = - app.world.get_resource_mut::>() + app.world.try_get_resource_mut::>() { if let Some(exit) = app_exit_event_reader.iter(&app_exit_events).last() { diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index c1656368b5818..c2fb35c1951fd 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -794,23 +794,19 @@ mod test { app.add_system(update_asset_storage_system::.after(FreeUnusedAssets)); fn load_asset(path: AssetPath, world: &World) -> HandleUntyped { - let asset_server = world.get_resource::().unwrap(); + let asset_server = world.get_resource::(); let id = futures_lite::future::block_on(asset_server.load_async(path.clone(), true)) .unwrap(); asset_server.get_handle_untyped(id) } fn get_asset(id: impl Into, world: &World) -> Option<&PngAsset> { - world - .get_resource::>() - .unwrap() - .get(id.into()) + world.get_resource::>().get(id.into()) } fn get_load_state(id: impl Into, world: &World) -> LoadState { world .get_resource::() - .unwrap() .get_load_state(id.into()) } diff --git a/crates/bevy_asset/src/assets.rs b/crates/bevy_asset/src/assets.rs index a848132363a2d..d598359d8d82e 100644 --- a/crates/bevy_asset/src/assets.rs +++ b/crates/bevy_asset/src/assets.rs @@ -287,7 +287,7 @@ impl AddAsset for App { return self; } let assets = { - let asset_server = self.world.get_resource::().unwrap(); + let asset_server = self.world.get_resource::(); asset_server.register_asset_type::() }; @@ -344,7 +344,6 @@ impl AddAsset for App { { self.world .get_resource_mut::() - .expect("AssetServer does not exist. Consider adding it as a resource.") .add_loader(loader); self } @@ -367,10 +366,7 @@ macro_rules! load_internal_asset { $path_str, ); } - let mut assets = $app - .world - .get_resource_mut::>() - .unwrap(); + let mut assets = $app.world.get_resource_mut::>(); assets.set_untracked($handle, ($loader)(include_str!($path_str))); }}; } @@ -379,10 +375,7 @@ macro_rules! load_internal_asset { #[macro_export] macro_rules! load_internal_asset { ($app: ident, $handle: ident, $path_str: expr, $loader: expr) => {{ - let mut assets = $app - .world - .get_resource_mut::>() - .unwrap(); + let mut assets = $app.world.get_resource_mut::>(); assets.set_untracked($handle, ($loader)(include_str!($path_str))); }}; } @@ -402,10 +395,10 @@ mod tests { app.add_plugin(bevy_core::CorePlugin) .add_plugin(crate::AssetPlugin); app.add_asset::(); - let mut assets_before = app.world.get_resource_mut::>().unwrap(); + let mut assets_before = app.world.get_resource_mut::>(); let handle = assets_before.add(MyAsset); app.add_asset::(); // Ensure this doesn't overwrite the Asset - let assets_after = app.world.get_resource_mut::>().unwrap(); + let assets_after = app.world.get_resource_mut::>(); assert!(assets_after.get(handle).is_some()); } } diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 6e31a62ea6546..6572ec30b248c 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -81,13 +81,8 @@ pub fn create_platform_default_asset_io(app: &mut App) -> Box { impl Plugin for AssetPlugin { fn build(&self, app: &mut App) { - if app.world.get_resource::().is_none() { - let task_pool = app - .world - .get_resource::() - .expect("`IoTaskPool` resource not found.") - .0 - .clone(); + if !app.world.contains_resource::() { + let task_pool = app.world.get_resource::().0.clone(); let source = create_platform_default_asset_io(app); diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index f943b281426cf..4ca0868405f0b 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -43,7 +43,7 @@ impl Plugin for CorePlugin { fn build(&self, app: &mut App) { // Setup the default bevy task pools app.world - .get_resource::() + .try_get_resource::() .cloned() .unwrap_or_default() .create_default_pools(&mut app.world); diff --git a/crates/bevy_core/src/time/fixed_timestep.rs b/crates/bevy_core/src/time/fixed_timestep.rs index fa39ced311997..76c80afa7699d 100644 --- a/crates/bevy_core/src/time/fixed_timestep.rs +++ b/crates/bevy_core/src/time/fixed_timestep.rs @@ -209,7 +209,7 @@ impl System for FixedTimestep { ))); self.internal_system.initialize(world); if let Some(ref label) = self.state.label { - let mut fixed_timesteps = world.get_resource_mut::().unwrap(); + let mut fixed_timesteps = world.get_resource_mut::(); fixed_timesteps.fixed_timesteps.insert( label.clone(), FixedTimestepState { @@ -257,25 +257,25 @@ mod test { // if time does not progress, the step does not run schedule.run(&mut world); schedule.run(&mut world); - assert_eq!(0, *world.get_resource::().unwrap()); + assert_eq!(0, *world.get_resource::()); assert_eq!(0., get_accumulator_deciseconds(&world)); // let's progress less than one step advance_time(&mut world, instance, 0.4); schedule.run(&mut world); - assert_eq!(0, *world.get_resource::().unwrap()); + assert_eq!(0, *world.get_resource::()); assert_eq!(4., get_accumulator_deciseconds(&world)); // finish the first step with 0.1s above the step length advance_time(&mut world, instance, 0.6); schedule.run(&mut world); - assert_eq!(1, *world.get_resource::().unwrap()); + assert_eq!(1, *world.get_resource::()); assert_eq!(1., get_accumulator_deciseconds(&world)); // runs multiple times if the delta is multiple step lengths advance_time(&mut world, instance, 1.7); schedule.run(&mut world); - assert_eq!(3, *world.get_resource::().unwrap()); + assert_eq!(3, *world.get_resource::()); assert_eq!(2., get_accumulator_deciseconds(&world)); } @@ -286,14 +286,12 @@ mod test { fn advance_time(world: &mut World, instance: Instant, seconds: f32) { world .get_resource_mut::