Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an init_bundle method to World #12573

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,18 @@ 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<B: Bundle>(&mut self) -> &[ComponentId] {
let id = self
.bundles
.init_info::<B>(&mut self.components, &mut self.storages);
self.bundles.get(id).unwrap().components()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.bundles.get(id).unwrap().components()
// SAFETY: The bundle was just initialized. The bundle must still exist.
unsafe { self.bundles.get(id).debug_unwrap_unchecked().components() }

We should be avoid panicking in the ECS when we know the invariants are satisfied. Ideally init_info should return a &BundleInfo to avoid the need for this, but that's out of the scope of the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to unwrap_unchecked, I couldn't find a debug_unwrap_unchecked anywhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DebugCheckedUnwrap is an extension trait we have inside bevy_ecs. It's identical but panics in debug mode if the Option isn't actually Some.

}
}

impl World {
Expand Down