From 1da7aba40ec2da1de50190c30c044b17024ec8ef Mon Sep 17 00:00:00 2001 From: William Rose Date: Tue, 19 Mar 2024 15:26:42 +0000 Subject: [PATCH 1/5] Added the `init_bundle` method --- crates/bevy_ecs/src/world/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 3f87d8026db27..d797294ffb9ec 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -2085,6 +2085,19 @@ impl World { self.storages.resources.clear(); self.storages.non_send_resources.clear(); } + + /// Initializes all of the components in the given [`Bundle`] and returns their ids. + /// + /// This is equivalent to calling [`init_component`](Self::init_component) on each component + /// in the bundle. + #[inline] + pub fn init_bundle(&mut self) -> Vec { + let mut ids = Vec::new(); + B::component_ids(&mut self.components, &mut self.storages, &mut |id| { + ids.push(id); + }); + ids + } } impl World { From 131bb373258461e9ed3c1faec0e91671de69a84b Mon Sep 17 00:00:00 2001 From: William Rose Date: Wed, 20 Mar 2024 19:11:54 +0000 Subject: [PATCH 2/5] Changed `init_bundle` to use `Bundles.init_info` and therefore instead return a &[ComponentId] --- crates/bevy_ecs/src/world/mod.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index d797294ffb9ec..07adb1753bff5 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -2091,12 +2091,11 @@ impl World { /// This is equivalent to calling [`init_component`](Self::init_component) on each component /// in the bundle. #[inline] - pub fn init_bundle(&mut self) -> Vec { - let mut ids = Vec::new(); - B::component_ids(&mut self.components, &mut self.storages, &mut |id| { - ids.push(id); - }); - ids + pub fn init_bundle(&mut self) -> &[ComponentId] { + let id = self + .bundles + .init_info::(&mut self.components, &mut self.storages); + self.bundles.get(id).unwrap().components() } } From ea84cd1528c921ea4bc4ddf042abbb73457dcc7e Mon Sep 17 00:00:00 2001 From: William Rose Date: Sun, 24 Mar 2024 20:03:20 +0000 Subject: [PATCH 3/5] Change to unwrap_unchecked because it should definitely be safe and this removes the chance of a panic --- crates/bevy_ecs/src/world/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 07adb1753bff5..cf1246a693065 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -2095,7 +2095,8 @@ impl World { let id = self .bundles .init_info::(&mut self.components, &mut self.storages); - self.bundles.get(id).unwrap().components() + // SAFETY: We just initialised the bundle so its id should definitely be valid. + unsafe { self.bundles.get(id).unwrap_unchecked().components() } } } From 8b6ee8a5d2acceef4fd31a42e2a6c3c2d6617388 Mon Sep 17 00:00:00 2001 From: William Rose Date: Sun, 24 Mar 2024 20:06:06 +0000 Subject: [PATCH 4/5] Changed init_bundle to return a `BundleInfo` rather than only the component ids to give the end user more flexibility --- crates/bevy_ecs/src/world/mod.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index cf1246a693065..60971bf0beff0 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -18,7 +18,7 @@ pub use spawn_batch::*; use crate::{ archetype::{ArchetypeComponentId, ArchetypeId, ArchetypeRow, Archetypes}, - bundle::{Bundle, BundleInserter, BundleSpawner, Bundles}, + bundle::{Bundle, BundleInfo, BundleInserter, BundleSpawner, Bundles}, change_detection::{MutUntyped, TicksMut}, component::{ Component, ComponentDescriptor, ComponentHooks, ComponentId, ComponentInfo, ComponentTicks, @@ -2086,17 +2086,18 @@ impl World { self.storages.non_send_resources.clear(); } - /// Initializes all of the components in the given [`Bundle`] and returns their ids. + /// Initializes all of the components in the given [`Bundle`] and returns both the component + /// ids and the bundle id. /// - /// This is equivalent to calling [`init_component`](Self::init_component) on each component - /// in the bundle. + /// This is largely equivalent to calling [`init_component`](Self::init_component) on each + /// component in the bundle. #[inline] - pub fn init_bundle(&mut self) -> &[ComponentId] { + pub fn init_bundle(&mut self) -> &BundleInfo { let id = self .bundles .init_info::(&mut self.components, &mut self.storages); // SAFETY: We just initialised the bundle so its id should definitely be valid. - unsafe { self.bundles.get(id).unwrap_unchecked().components() } + unsafe { self.bundles.get(id).unwrap_unchecked() } } } From 50d2c5b7727056f7387ea6b0bcf8983a53916e32 Mon Sep 17 00:00:00 2001 From: James Liu Date: Sun, 24 Mar 2024 16:41:12 -0700 Subject: [PATCH 5/5] Update crates/bevy_ecs/src/world/mod.rs --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 60971bf0beff0..7231ac36c8367 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -2097,7 +2097,7 @@ impl World { .bundles .init_info::(&mut self.components, &mut self.storages); // SAFETY: We just initialised the bundle so its id should definitely be valid. - unsafe { self.bundles.get(id).unwrap_unchecked() } + unsafe { self.bundles.get(id).debug_checked_unwrap() } } }