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

Code before:
/// Used in [`ArchetypeAfterBundleInsert`] to track whether components in the bundle are newly
/// added or already existed in the entity's archetype.
#[derive(Copy, Clone, Eq, PartialEq)]
pub(crate) enum ComponentStatus {
    Added,
    Existing,
}

/// 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;

Review comment (suggestion):
We could shave another 4 bytes off by changing this to a u32. It'd fit in the space used for padding the ArchetypeId.

Code after:

/// Used in [`ArchetypeAfterBundleInsert`] to track whether components in the bundle are newly
/// added or already existed in the entity's archetype.
#[derive(Copy, Clone, Eq, PartialEq)]
pub(crate) enum ComponentStatus {
    Added,
    Existing,
}

/// 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