Repository: bevyengine/bevy
File: crates/bevy_ecs/src/archetype.rs (Rust)

Code before:
/// Used in [`Edges`] to cache the result of inserting a bundle into the source archetype.
pub(crate) struct ArchetypeAfterBundleInsert {
    /// The target archetype after the bundle is inserted into the source archetype.
    pub archetype_id: ArchetypeId,
    /// For each component iterated in the same order as the source [`Bundle`](crate::bundle::Bundle),
    /// indicate if the component is newly added to the target archetype or if it already existed.
    bundle_status: Box<[ComponentStatus]>,
    /// The set of additional required components that must be initialized immediately when adding this Bundle.
    ///
    /// The initial values are determined based on the provided constructor, falling back to the `Default` trait if none is given.
    pub required_components: Box<[RequiredComponentConstructor]>,
    /// The components inserted by this bundle, with added components before existing ones.
    /// Added components includes any Required Components that are inserted when adding this bundle,
    /// but existing components only includes ones explicitly contributed by this bundle.
    inserted: Box<[ComponentId]>,
    /// The number of components added by this bundle, including Required Components.
    added_len: usize,
}

impl ArchetypeAfterBundleInsert {
    pub(crate) fn inserted(&self) -> &[ComponentId] {
        &self.inserted
    }

    pub(crate) fn added(&self) -> &[ComponentId] {
        &self.inserted[..self.added_len]
    }

    pub(crate) fn existing(&self) -> &[ComponentId] {
        &self.inserted[self.added_len..]
    }
}

/// This trait is used to report the status of [`Bundle`](crate::bundle::Bundle) components
/// being inserted into a given entity, relative to that entity's original archetype.
/// See [`crate::bundle::BundleInfo::write_components`] for more info.
pub(crate) trait BundleComponentStatus {
    /// Returns the Bundle's component status for the given "bundle index".
    ///
    /// # Safety
    /// Callers must ensure that index is always a valid bundle index for the
    /// Bundle associated with this [`BundleComponentStatus`]
    unsafe fn get_status(&self, index: usize) -> ComponentStatus;
}

impl BundleComponentStatus for ArchetypeAfterBundleInsert {
    #[inline]
    unsafe fn get_status(&self, index: usize) -> ComponentStatus {
        // SAFETY: caller has ensured index is a valid bundle index for this bundle
        unsafe { *self.bundle_status.get_unchecked(index) }
    }

Review comment (bug):
This introduces extra panic codegen in some already panic heavy codepaths. Perhaps use `Vec::get` with the range, and DebugCheckedUnwrap? We always know that added_len must be within bounds.

Code after:

/// Used in [`Edges`] to cache the result of inserting a bundle into the source archetype.
pub(crate) struct ArchetypeAfterBundleInsert {
    /// The target archetype after the bundle is inserted into the source archetype.
    pub archetype_id: ArchetypeId,
    /// For each component iterated in the same order as the source [`Bundle`](crate::bundle::Bundle),
    /// indicate if the component is newly added to the target archetype or if it already existed.
    bundle_status: Box<[ComponentStatus]>,
    /// The set of additional required components that must be initialized immediately when adding this Bundle.
    ///
    /// The initial values are determined based on the provided constructor, falling back to the `Default` trait if none is given.
    pub required_components: Box<[RequiredComponentConstructor]>,
    /// The components inserted by this bundle, with added components before existing ones.
    /// Added components includes any Required Components that are inserted when adding this bundle,
    /// but existing components only includes ones explicitly contributed by this bundle.
    inserted: Box<[ComponentId]>,
    /// The number of components added by this bundle, including Required Components.
    added_len: usize,
}

impl ArchetypeAfterBundleInsert {
    pub(crate) fn inserted(&self) -> &[ComponentId] {
        &self.inserted
    }

    pub(crate) fn added(&self) -> &[ComponentId] {
        // SAFETY: `added_len` is always in range `0..=inserted.len()`
        unsafe { self.inserted.get(..self.added_len).debug_checked_unwrap() }
    }

    pub(crate) fn existing(&self) -> &[ComponentId] {
        // SAFETY: `added_len` is always in range `0..=inserted.len()`
        unsafe { self.inserted.get(self.added_len..).debug_checked_unwrap() }
    }
}

/// This trait is used to report the status of [`Bundle`](crate::bundle::Bundle) components
/// being inserted into a given entity, relative to that entity's original archetype.
/// See [`crate::bundle::BundleInfo::write_components`] for more info.
pub(crate) trait BundleComponentStatus {
    /// Returns the Bundle's component status for the given "bundle index".
    ///
    /// # Safety
    /// Callers must ensure that index is always a valid bundle index for the
    /// Bundle associated with this [`BundleComponentStatus`]
    unsafe fn get_status(&self, index: usize) -> ComponentStatus;
}

impl BundleComponentStatus for ArchetypeAfterBundleInsert {
    #[inline]
    unsafe fn get_status(&self, index: usize) -> ComponentStatus {