diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 176c5caf60a56..8412d2df2235d 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -397,6 +397,40 @@ impl App { self } + /// Inserts the [`Resource`], initialized with its default value, into the app, + /// if there is no existing instance of `R`. + /// + /// `R` must implement [`FromWorld`]. + /// If `R` implements [`Default`], [`FromWorld`] will be automatically implemented and + /// initialize the [`Resource`] with [`Default::default`]. + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// #[derive(Resource)] + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// impl Default for MyCounter { + /// fn default() -> MyCounter { + /// MyCounter { + /// counter: 100 + /// } + /// } + /// } + /// + /// App::new() + /// .init_resource::(); + /// ``` + pub fn init_resource(&mut self) -> &mut Self { + self.main_mut().init_resource::(); + self + } + /// Inserts the [`Resource`] into the app, overwriting any existing resource of the same type. /// /// There is also an [`init_resource`](Self::init_resource) for resources that have @@ -421,12 +455,93 @@ impl App { self } - /// Inserts the [`Resource`], initialized with its default value, into the app, + /// Inserts the [`!Send`](Send) resource into the app, initialized with its default value, /// if there is no existing instance of `R`. + pub fn init_non_send_resource(&mut self) -> &mut Self { + self.main_mut().init_non_send_resource::(); + self + } + + /// Inserts the [`!Send`](Send) resource into the app, overwriting any existing resource + /// of the same type. /// - /// `R` must implement [`FromWorld`]. - /// If `R` implements [`Default`], [`FromWorld`] will be automatically implemented and - /// initialize the [`Resource`] with [`Default::default`]. + /// There is also an [`init_non_send_resource`](Self::init_non_send_resource) for + /// resources that implement [`Default`] + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// App::new() + /// .insert_non_send_resource(MyCounter { counter: 0 }); + /// ``` + pub fn insert_non_send_resource(&mut self, resource: R) -> &mut Self { + self.main_mut().insert_non_send_resource(resource); + self + } + + /// Removes the resource of a given type and returns it, if it exists. Otherwise, returns `None`. + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// #[derive(Resource)] + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_resource(MyCounter { counter: 0 }); + /// + /// assert!(app.remove_resource::().is_some()); + /// ``` + #[inline] + pub fn remove_resource(&mut self) -> Option { + self.main_mut().remove_resource() + } + + /// Removes a `!Send` resource from the world and returns it, if present. + /// + /// `NonSend` resources cannot be sent across threads, + /// and do not need the `Send + Sync` bounds. + /// Systems with `NonSend` resources are always scheduled on the main thread. + /// + /// Returns `None` if a value was not previously present. + /// + /// # Panics + /// If a value is present, this function will panic if called from a different + /// thread than where the value was inserted from. + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_non_send_resource(MyCounter { counter: 0 }); + /// + /// assert!(app.remove_non_send_resource::().is_some()); + /// ``` + #[inline] + pub fn remove_non_send_resource(&mut self) -> Option { + self.main_mut().remove_non_send_resource() + } + + /// Returns `true` if the [`Resource`] exists in the app world. /// /// # Examples /// @@ -447,19 +562,45 @@ impl App { /// } /// } /// - /// App::new() - /// .init_resource::(); + /// let mut app = App::new(); + /// app.insert_resource(MyCounter { counter: 0 }); + /// + /// assert!(app.contains_resource::()); /// ``` - pub fn init_resource(&mut self) -> &mut Self { - self.main_mut().init_resource::(); - self + pub fn contains_resource(&self) -> bool { + self.main().contains_resource::() } - /// Inserts the [`!Send`](Send) resource into the app, overwriting any existing resource - /// of the same type. + /// Returns `true` if the [`!Send`](Send) resource exists in the app world. /// - /// There is also an [`init_non_send_resource`](Self::init_non_send_resource) for - /// resources that implement [`Default`] + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_non_send_resource(MyCounter { counter: 0 }); + /// + /// assert!(app.contains_non_send_resource::()); + /// ``` + pub fn contains_non_send_resource(&self) -> bool { + self.main().contains_non_send_resource::() + } + + /// Gets a reference to the resource of the given type + /// + /// # Panics + /// + /// Panics if the resource does not exist. + /// Use [`get_resource`](App::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`](App::get_resource_or_insert_with). /// /// # Examples /// @@ -467,23 +608,265 @@ impl App { /// # use bevy_app::prelude::*; /// # use bevy_ecs::prelude::*; /// # + /// #[derive(Resource)] /// struct MyCounter { /// counter: usize, /// } /// - /// App::new() - /// .insert_non_send_resource(MyCounter { counter: 0 }); + /// let mut app = App::new(); + /// app.insert_resource(MyCounter { counter: 0 }); + /// + /// assert_eq!(app.resource::().counter, 0); + pub fn resource(&self) -> &R { + self.main().resource() + } + + /// Gets a reference to the resource of the given type + /// + /// # Panics + /// + /// Panics if the resource does not exist. + /// Use [`get_resource_ref`](App::get_resource_ref) 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`](App::get_resource_or_insert_with). + /// + /// # Examples + /// /// ``` - pub fn insert_non_send_resource(&mut self, resource: R) -> &mut Self { - self.world_mut().insert_non_send_resource(resource); - self + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// #[derive(Resource)] + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_resource(MyCounter { counter: 0 }); + /// + /// assert_eq!(app.resource_ref::().counter, 0); + pub fn resource_ref(&self) -> Res { + self.main().resource_ref() } - /// Inserts the [`!Send`](Send) resource into the app, initialized with its default value, - /// if there is no existing instance of `R`. - pub fn init_non_send_resource(&mut self) -> &mut Self { - self.world_mut().init_non_send_resource::(); - self + /// Gets a mutable reference to the resource of the given type + /// + /// # Panics + /// + /// Panics if the resource does not exist. + /// Use [`get_resource_mut`](App::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`](App::get_resource_or_insert_with). + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// #[derive(Resource)] + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_resource(MyCounter { counter: 0 }); + /// app.resource_mut::().counter = 1; + /// + /// assert_eq!(app.resource::().counter, 1); + pub fn resource_mut(&mut self) -> Mut<'_, R> { + self.main_mut().resource_mut() + } + + /// Gets a reference to the resource of the given type if it exists + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// #[derive(Resource)] + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_resource(MyCounter { counter: 0 }); + /// + /// assert!(app.get_resource::().is_some()); + pub fn get_resource(&self) -> Option<&R> { + self.main().get_resource() + } + + /// Gets a reference including change detection to the resource of the given type if it exists. + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// #[derive(Resource)] + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_resource(MyCounter { counter: 0 }); + /// + /// assert!(app.get_resource_ref::().is_some()); + pub fn get_resource_ref(&self) -> Option> { + self.main().get_resource_ref() + } + + /// Gets a mutable reference to the resource of the given type if it exists + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// #[derive(Resource)] + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_resource(MyCounter { counter: 0 }); + /// app.get_resource_mut::().unwrap().counter = 1; + /// + /// assert_eq!(app.resource::().counter, 1); + pub fn get_resource_mut(&mut self) -> Option> { + self.main_mut().get_resource_mut() + } + + /// Gets a mutable reference to the resource of type `T` if it exists, + /// otherwise inserts the resource using the result of calling `func`. + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # + /// #[derive(Resource)] + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.get_resource_or_insert_with(|| MyCounter { counter: 0 }); + /// + /// assert!(app.get_resource::().is_some()); + pub fn get_resource_or_insert_with( + &mut self, + func: impl FnOnce() -> R, + ) -> Mut<'_, R> { + self.main_mut().get_resource_or_insert_with(func) + } + + /// Gets an immutable reference to the non-send resource of the given type, if it exists. + /// + /// # Panics + /// + /// Panics if the resource does not exist. + /// Use [`get_non_send_resource`](App::get_non_send_resource) instead if you want to handle this case. + /// + /// This function will panic if it isn't called from the same thread that the resource was inserted from. + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_non_send_resource(MyCounter { counter: 0 }); + /// + /// assert_eq!(app.non_send_resource::().counter, 0); + pub fn non_send_resource(&self) -> &R { + self.main().non_send_resource() + } + + /// Gets a mutable reference to the non-send resource of the given type, if it exists. + /// + /// # Panics + /// + /// Panics if the resource does not exist. + /// Use [`get_non_send_resource_mut`](App::get_non_send_resource_mut) instead if you want to handle this case. + /// + /// This function will panic if it isn't called from the same thread that the resource was inserted from. + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_non_send_resource(MyCounter { counter: 0 }); + /// app.non_send_resource_mut::().counter = 1; + /// + /// assert_eq!(app.non_send_resource::().counter, 1); + pub fn non_send_resource_mut(&mut self) -> Mut<'_, R> { + self.main_mut().non_send_resource_mut() + } + + /// Gets a reference to the non-send resource of the given type, if it exists. + /// Otherwise, returns `None`. + /// + /// # Panics + /// This function will panic if it isn't called from the same thread that the resource was inserted from. + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_non_send_resource(MyCounter { counter: 0 }); + /// + /// assert!(app.get_non_send_resource::().is_some()); + pub fn get_non_send_resource(&self) -> Option<&R> { + self.main().get_non_send_resource() + } + + /// Gets a mutable reference to the non-send resource of the given type, if it exists. + /// Otherwise, returns `None`. + /// + /// # Panics + /// This function will panic if it isn't called from the same thread that the resource was inserted from. + /// + /// # Examples + /// + /// ``` + /// # use bevy_app::prelude::*; + /// # + /// struct MyCounter { + /// counter: usize, + /// } + /// + /// let mut app = App::new(); + /// app.insert_non_send_resource(MyCounter { counter: 0 }); + /// app.get_non_send_resource_mut::().unwrap().counter = 1; + /// + /// assert_eq!(app.non_send_resource::().counter, 1); + pub fn get_non_send_resource_mut(&mut self) -> Option> { + self.main_mut().get_non_send_resource_mut() } pub(crate) fn add_boxed_plugin( @@ -1202,7 +1585,7 @@ mod tests { fn my_runner(mut app: App) -> AppExit { let my_state = MyState {}; - app.world_mut().insert_resource(my_state); + app.insert_resource(my_state); for _ in 0..5 { app.update(); diff --git a/crates/bevy_app/src/sub_app.rs b/crates/bevy_app/src/sub_app.rs index b6974cc60f7e0..d5bee7645751f 100644 --- a/crates/bevy_app/src/sub_app.rs +++ b/crates/bevy_app/src/sub_app.rs @@ -174,6 +174,96 @@ impl SubApp { self } + /// See [`App::insert_non_resource`]. + pub fn insert_non_send_resource(&mut self, resource: R) -> &mut Self { + self.world.insert_non_send_resource(resource); + self + } + + /// See [`App::init_non_send_resource`]. + pub fn init_non_send_resource(&mut self) -> &mut Self { + self.world.init_non_send_resource::(); + self + } + + /// See [`App::remove_resource`]. + pub fn remove_resource(&mut self) -> Option { + self.world.remove_resource::() + } + + /// See [`App::remove_non_send_resource`]. + pub fn remove_non_send_resource(&mut self) -> Option { + self.world.remove_non_send_resource::() + } + + /// See [`App::contains_resource`]. + pub fn contains_resource(&self) -> bool { + self.world.contains_resource::() + } + + /// See [`App::contains_non_send_resource`]. + pub fn contains_non_send_resource(&self) -> bool { + self.world.contains_non_send_resource::() + } + + /// See [`App::resource`]. + pub fn resource(&self) -> &R { + self.world.resource() + } + + /// See [`App::resource_ref`]. + pub fn resource_ref(&self) -> Res { + self.world.resource_ref() + } + + /// See [`App::resource_mut`]. + pub fn resource_mut(&mut self) -> Mut<'_, R> { + self.world.resource_mut() + } + + /// See [`App::get_resource`]. + pub fn get_resource(&self) -> Option<&R> { + self.world.get_resource() + } + + /// See [`App::get_resource_ref`]. + pub fn get_resource_ref(&self) -> Option> { + self.world.get_resource_ref() + } + + /// See [`App::get_resource_mut`]. + pub fn get_resource_mut(&mut self) -> Option> { + self.world.get_resource_mut() + } + + /// See [`App::get_resource_or_insert_with`]. + pub fn get_resource_or_insert_with( + &mut self, + func: impl FnOnce() -> R, + ) -> Mut<'_, R> { + self.world.get_resource_or_insert_with(func) + } + + /// See [`App::non_send_resource`]. + pub fn non_send_resource(&self) -> &R { + self.world.non_send_resource() + } + + /// See [`App::non_send_resource_mut`]. + pub fn non_send_resource_mut(&mut self) -> Mut<'_, R> { + self.world.non_send_resource_mut() + } + + /// See [`App::get_non_send_resource`]. + pub fn get_non_send_resource(&self) -> Option<&R> { + self.world.get_non_send_resource() + } + + /// See [`App::get_non_send_resource_mut`]. + pub fn get_non_send_resource_mut(&mut self) -> Option> { + self.world.get_non_send_resource_mut() + } + /// See [`App::add_systems`]. pub fn add_systems( &mut self, diff --git a/crates/bevy_asset/src/io/embedded/mod.rs b/crates/bevy_asset/src/io/embedded/mod.rs index 018fb9ed9a6ff..26ce0378b3421 100644 --- a/crates/bevy_asset/src/io/embedded/mod.rs +++ b/crates/bevy_asset/src/io/embedded/mod.rs @@ -253,7 +253,7 @@ pub fn watched_path(_source_file_path: &'static str, _asset_path: &'static str) #[macro_export] macro_rules! load_internal_asset { ($app: ident, $handle: expr, $path_str: expr, $loader: expr) => {{ - let mut assets = $app.world_mut().resource_mut::<$crate::Assets<_>>(); + let mut assets = $app.resource_mut::<$crate::Assets<_>>(); assets.insert($handle.id(), ($loader)( include_str!($path_str), std::path::Path::new(file!()) @@ -265,7 +265,7 @@ macro_rules! load_internal_asset { }}; // we can't support params without variadic arguments, so internal assets with additional params can't be hot-reloaded ($app: ident, $handle: ident, $path_str: expr, $loader: expr $(, $param:expr)+) => {{ - let mut assets = $app.world_mut().resource_mut::<$crate::Assets<_>>(); + let mut assets = $app.resource_mut::<$crate::Assets<_>>(); assets.insert($handle.id(), ($loader)( include_str!($path_str), std::path::Path::new(file!()) @@ -282,7 +282,7 @@ macro_rules! load_internal_asset { #[macro_export] macro_rules! load_internal_binary_asset { ($app: ident, $handle: expr, $path_str: expr, $loader: expr) => {{ - let mut assets = $app.world_mut().resource_mut::<$crate::Assets<_>>(); + let mut assets = $app.resource_mut::<$crate::Assets<_>>(); assets.insert( $handle.id(), ($loader)( diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index ea8caf003a1dd..40d326cf38e75 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -170,7 +170,7 @@ impl Plugin for AssetPlugin { } match self.mode { AssetMode::Unprocessed => { - let mut builders = app.world_mut().resource_mut::(); + let mut builders = app.resource_mut::(); let sources = builders.build_sources(watch, false); app.insert_resource(AssetServer::new_with_meta_check( @@ -183,7 +183,7 @@ impl Plugin for AssetPlugin { AssetMode::Processed => { #[cfg(feature = "asset_processor")] { - let mut builders = app.world_mut().resource_mut::(); + let mut builders = app.resource_mut::(); let processor = AssetProcessor::new(&mut builders); let mut sources = builders.build_sources(false, watch); sources.gate_on_processor(processor.data.clone()); @@ -200,7 +200,7 @@ impl Plugin for AssetPlugin { } #[cfg(not(feature = "asset_processor"))] { - let mut builders = app.world_mut().resource_mut::(); + let mut builders = app.resource_mut::(); let sources = builders.build_sources(false, watch); app.insert_resource(AssetServer::new_with_meta_check( sources, @@ -364,7 +364,7 @@ impl AssetApp for App { self.world() .resource::() .register_asset(&assets); - if self.world().contains_resource::() { + if self.contains_resource::() { let processor = self.world().resource::(); // The processor should have its own handle provider separate from the Asset storage // to ensure the id spaces are entirely separate. Not _strictly_ necessary, but @@ -906,7 +906,7 @@ mod tests { }); { - let mut texts = app.world_mut().resource_mut::>(); + let mut texts = app.resource_mut::>(); let a = texts.get_mut(a_id).unwrap(); a.text = "Changed".to_string(); } @@ -925,8 +925,8 @@ mod tests { 0, "SubText asset entities should be despawned when no more handles exist" ); - let events = app.world_mut().remove_resource::().unwrap(); - let id_results = app.world_mut().remove_resource::().unwrap(); + let events = app.remove_resource::().unwrap(); + let id_results = app.remove_resource::().unwrap(); let expected_events = vec![ AssetEvent::Added { id: a_id }, AssetEvent::LoadedWithDependencies { @@ -1190,7 +1190,7 @@ mod tests { ); // remove event is emitted app.update(); - let events = std::mem::take(&mut app.world_mut().resource_mut::().0); + let events = std::mem::take(&mut app.resource_mut::().0); let expected_events = vec![ AssetEvent::Added { id }, AssetEvent::Unused { id }, @@ -1211,14 +1211,14 @@ mod tests { // TODO: ideally it doesn't take two updates for the added event to emit app.update(); - let events = std::mem::take(&mut app.world_mut().resource_mut::().0); + let events = std::mem::take(&mut app.resource_mut::().0); let expected_events = vec![AssetEvent::Added { id: a_handle.id() }]; assert_eq!(events, expected_events); gate_opener.open(dep_path); loop { app.update(); - let events = std::mem::take(&mut app.world_mut().resource_mut::().0); + let events = std::mem::take(&mut app.resource_mut::().0); if events.is_empty() { continue; } @@ -1232,7 +1232,7 @@ mod tests { break; } app.update(); - let events = std::mem::take(&mut app.world_mut().resource_mut::().0); + let events = std::mem::take(&mut app.resource_mut::().0); let expected_events = vec![AssetEvent::Added { id: dep_handle.id(), }]; diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index ea52da782287b..44efbf368f744 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1189,7 +1189,7 @@ impl World { }); } - /// Removes the resource of a given type and returns it, if it exists. Otherwise returns `None`. + /// Removes the resource of a given type and returns it, if it exists. Otherwise, returns `None`. #[inline] pub fn remove_resource(&mut self) -> Option { let component_id = self.components.get_resource_id(TypeId::of::())?; @@ -1221,7 +1221,7 @@ impl World { unsafe { Some(ptr.read::()) } } - /// Returns `true` if a resource of type `R` exists. Otherwise returns `false`. + /// Returns `true` if a resource of type `R` exists. Otherwise, returns `false`. #[inline] pub fn contains_resource(&self) -> bool { self.components @@ -1231,9 +1231,16 @@ impl World { .unwrap_or(false) } - /// Returns `true` if a resource of type `R` exists. Otherwise returns `false`. + /// Returns `true` if a resource of type `R` exists. Otherwise, returns `false`. #[inline] + #[deprecated = "Use `World::contains_non_send_resource` instead"] pub fn contains_non_send(&self) -> bool { + self.contains_non_send_resource::() + } + + /// Returns `true` if a resource of type `R` exists. Otherwise, returns `false`. + #[inline] + pub fn contains_non_send_resource(&self) -> bool { self.components .get_resource_id(TypeId::of::()) .and_then(|component_id| self.storages.non_send_resources.get(component_id)) @@ -1501,7 +1508,7 @@ impl World { } /// Gets a reference to the non-send resource of the given type, if it exists. - /// Otherwise returns `None`. + /// Otherwise, returns `None`. /// /// # Panics /// This function will panic if it isn't called from the same thread that the resource was inserted from. @@ -1514,7 +1521,7 @@ impl World { } /// Gets a mutable reference to the non-send resource of the given type, if it exists. - /// Otherwise returns `None`. + /// Otherwise, returns `None`. /// /// # Panics /// This function will panic if it isn't called from the same thread that the resource was inserted from. @@ -2379,7 +2386,7 @@ impl World { } } - /// Removes the resource of a given type, if it exists. Otherwise returns `None`. + /// Removes the resource of a given type, if it exists. Otherwise, returns `None`. /// /// **You should prefer to use the typed API [`World::remove_resource`] where possible and only /// use this in cases where the actual types are not known at compile time.** @@ -2391,7 +2398,7 @@ impl World { Some(()) } - /// Removes the resource of a given type, if it exists. Otherwise returns `None`. + /// Removes the resource of a given type, if it exists. Otherwise, returns `None`. /// /// **You should prefer to use the typed API [`World::remove_resource`] where possible and only /// use this in cases where the actual types are not known at compile time.** diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index d523f0dd5403f..bbb94069db259 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -206,7 +206,7 @@ pub trait AppGizmoBuilder { impl AppGizmoBuilder for App { fn init_gizmo_group(&mut self) -> &mut Self { - if self.world().contains_resource::>() { + if self.contains_resource::>() { return self; } diff --git a/crates/bevy_pbr/src/lib.rs b/crates/bevy_pbr/src/lib.rs index 8c5078f1fe245..14856d67427d2 100644 --- a/crates/bevy_pbr/src/lib.rs +++ b/crates/bevy_pbr/src/lib.rs @@ -401,7 +401,7 @@ impl Plugin for PbrPlugin { .init_resource::(); let shadow_pass_node = ShadowPassNode::new(render_app.world_mut()); - let mut graph = render_app.world_mut().resource_mut::(); + let mut graph = render_app.resource_mut::(); let draw_3d_graph = graph.get_sub_graph_mut(Core3d).unwrap(); draw_3d_graph.add_node(NodePbr::ShadowPass, shadow_pass_node); draw_3d_graph.add_node_edge(NodePbr::ShadowPass, Node3d::StartMainPass); diff --git a/crates/bevy_render/src/camera/mod.rs b/crates/bevy_render/src/camera/mod.rs index 83c882cc3ed8e..ea3899d929698 100644 --- a/crates/bevy_render/src/camera/mod.rs +++ b/crates/bevy_render/src/camera/mod.rs @@ -47,7 +47,7 @@ impl Plugin for CameraPlugin { .add_systems(ExtractSchedule, extract_cameras) .add_systems(Render, sort_cameras.in_set(RenderSet::ManageViews)); let camera_driver_node = CameraDriverNode::new(render_app.world_mut()); - let mut render_graph = render_app.world_mut().resource_mut::(); + let mut render_graph = render_app.resource_mut::(); render_graph.add_node(crate::graph::CameraDriverLabel, camera_driver_node); } } diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index dea5f7bb7d38f..63d6773a9035d 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -368,7 +368,7 @@ impl Plugin for RenderPlugin { Shader::from_wgsl ); if let Some(future_renderer_resources) = - app.world_mut().remove_resource::() + app.remove_resource::() { let (device, queue, adapter_info, render_adapter, instance) = future_renderer_resources.0.lock().unwrap().take().unwrap(); diff --git a/crates/bevy_render/src/pipelined_rendering.rs b/crates/bevy_render/src/pipelined_rendering.rs index d0f31ad60579c..2e76810dd556e 100644 --- a/crates/bevy_render/src/pipelined_rendering.rs +++ b/crates/bevy_render/src/pipelined_rendering.rs @@ -132,7 +132,7 @@ impl Plugin for PipelinedRenderingPlugin { // clone main thread executor to render world let executor = app.world().get_resource::().unwrap(); - render_app.world_mut().insert_resource(executor.clone()); + render_app.insert_resource(executor.clone()); render_to_app_sender.send_blocking(render_app).unwrap(); diff --git a/crates/bevy_render/src/texture/mod.rs b/crates/bevy_render/src/texture/mod.rs index ac5d74cdc73b8..2486d852b5359 100644 --- a/crates/bevy_render/src/texture/mod.rs +++ b/crates/bevy_render/src/texture/mod.rs @@ -89,8 +89,7 @@ impl Plugin for ImagePlugin { .init_asset::() .register_asset_reflect::(); - app.world_mut() - .resource_mut::>() + app.resource_mut::>() .insert(&Handle::default(), Image::default()); #[cfg(feature = "basis-universal")] if let Some(processor) = app diff --git a/crates/bevy_scene/src/bundle.rs b/crates/bevy_scene/src/bundle.rs index a154acdccdaa5..d3dd1379e41a7 100644 --- a/crates/bevy_scene/src/bundle.rs +++ b/crates/bevy_scene/src/bundle.rs @@ -166,7 +166,7 @@ mod tests { ); // let's try to delete the scene - let mut scene_spawner = app.world_mut().resource_mut::(); + let mut scene_spawner = app.resource_mut::(); scene_spawner.despawn(&scene_handle); // run the scene spawner system to despawn the scene diff --git a/crates/bevy_sprite/src/mesh2d/color_material.rs b/crates/bevy_sprite/src/mesh2d/color_material.rs index 9bf6581b8848f..462f667dbc3e7 100644 --- a/crates/bevy_sprite/src/mesh2d/color_material.rs +++ b/crates/bevy_sprite/src/mesh2d/color_material.rs @@ -28,8 +28,7 @@ impl Plugin for ColorMaterialPlugin { app.add_plugins(Material2dPlugin::::default()) .register_asset_reflect::(); - app.world_mut() - .resource_mut::>() + app.resource_mut::>() .insert( &Handle::::default(), ColorMaterial { diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 3aaca1004ac21..02b88f794215c 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -121,7 +121,7 @@ pub fn build_ui_render(app: &mut App) { // Render graph let ui_graph_2d = get_ui_graph(render_app); let ui_graph_3d = get_ui_graph(render_app); - let mut graph = render_app.world_mut().resource_mut::(); + let mut graph = render_app.resource_mut::(); if let Some(graph_2d) = graph.get_sub_graph_mut(Core2d) { graph_2d.add_sub_graph(SubGraphUi, ui_graph_2d); diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index 7ecf8d21c0e02..fd929b9ffa870 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -114,7 +114,7 @@ impl Plugin for WindowPlugin { .spawn(primary_window.clone()) .insert(PrimaryWindow) .id(); - if let Some(mut focus) = app.world_mut().get_resource_mut::() { + if let Some(mut focus) = app.get_resource_mut::() { **focus = Some(initial_focus); } } diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 60794135e39aa..913bb33e60469 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -276,8 +276,7 @@ pub fn winit_runner(mut app: App) -> AppExit { .remove_non_send_resource::>() .unwrap(); - app.world_mut() - .insert_non_send_resource(event_loop.create_proxy()); + app.insert_non_send_resource(event_loop.create_proxy()); let mut runner_state = WinitAppRunnerState::default(); diff --git a/crates/bevy_winit/src/winit_event.rs b/crates/bevy_winit/src/winit_event.rs index a314ea289621e..2ea4d1dce0db9 100644 --- a/crates/bevy_winit/src/winit_event.rs +++ b/crates/bevy_winit/src/winit_event.rs @@ -274,7 +274,6 @@ pub(crate) fn forward_winit_events(buffered_events: &mut Vec, app: & } } } - app.world_mut() - .resource_mut::>() + app.resource_mut::>() .send_batch(buffered_events.drain(..)); }