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

Code before:
    pub struct SystemKey;
    /// A unique identifier for a system set in a [`ScheduleGraph`].
    pub struct SystemSetKey;
}

/// Systems and system sets in a [`ScheduleGraph`] which have not been
/// initialized yet.
///
/// We have to defer initialization of nodes in the graph until we have
/// `&mut World` access, so we store these in a list ([`ScheduleGraph::uninit`])
/// until then. In most cases, initialization occurs upon the first run of the
/// schedule.
enum UninitializedId {
    /// A system and its conditions that have not been initialized yet.
    System(SystemKey),
    /// A system set's conditions that have not been initialized yet.
    Set {
        key: SystemSetKey,
        /// The index in [`SystemSets::conditions`] where the first
        /// uninitialized condition is. All conditions after this index also
        /// still need to be initialized.
        ///
        /// Conditions might be added multiple times to a system set (e.g. when
        /// `configure_sets` is called multiple times with the same set), So we
        /// need to track where the newly added conditions start in the
        /// `conditions` list.
        ///
        /// Note: We don't need to do this for systems because calling
        /// `add_systems` multiple times with the same system results in
        /// multiple duplicates of that system in the graph, each with their own
        /// separate list of conditions. That means all of a system's conditions
        /// are always initialized together.
        first_uninit_condition: usize,
    },
}

/// Metadata for system sets in a schedule.
#[derive(Default)]
struct SystemSets {
    /// List of system sets in the schedule
    sets: SlotMap<SystemSetKey, SystemSetNode>,
    /// List of conditions for each system set, in the same order as `system_sets`
    conditions: SecondaryMap<SystemSetKey, Vec<ConditionWithAccess>>,
    /// Map from system set to node id
    ids: HashMap<InternedSystemSet, SystemSetKey>,
}

impl SystemSets {
    fn get_or_add_set(&mut self, set: InternedSystemSet) -> SystemSetKey {
        *self.ids.entry(set).or_insert_with(|| {
            let key = self.sets.insert(SystemSetNode::new(set));

Review comment (suggestion):
Not related to this PR, but if we call `configure_sets` twice *without* initializing in between, do we wind up initializing the later conditions twice?  It seems like this would need to be a `Range<usize>` to avoid that, but maybe I'm just missing something.  

Code after:
    /// A unique identifier for a system in a [`ScheduleGraph`].
    pub struct SystemKey;
    /// A unique identifier for a system set in a [`ScheduleGraph`].
    pub struct SystemSetKey;
}

/// A node in a [`ScheduleGraph`] with a system or conditions that have not been
/// initialized yet.
///
/// We have to defer initialization of nodes in the graph until we have
/// `&mut World` access, so we store these in a list ([`ScheduleGraph::uninit`])
/// until then. In most cases, initialization occurs upon the first run of the
/// schedule.
enum UninitializedId {
    /// A system and its conditions that have not been initialized yet.
    System(SystemKey),
    /// A system set's conditions that have not been initialized yet.
    Set {
        key: SystemSetKey,
        /// The range of indices in [`SystemSets::conditions`] that correspond
        /// to conditions that have not been initialized yet.
        ///
        /// [`SystemSets::conditions`] for a given set may be appended to
        /// multiple times (e.g. when `configure_sets` is called multiple with
        /// the same set), so we need to track which conditions in that list
        /// are newly added and not yet initialized.
        ///
        /// Systems don't need this tracking because each `add_systems` call
        /// creates separate nodes in the graph with their own conditions,
        /// so all conditions are initialized together.
        uninitialized_conditions: Range<usize>,
    },
}

/// Metadata for system sets in a schedule.
#[derive(Default)]
struct SystemSets {
    /// List of system sets in the schedule
    sets: SlotMap<SystemSetKey, SystemSetNode>,
    /// List of conditions for each system set, in the same order as `system_sets`
    conditions: SecondaryMap<SystemSetKey, Vec<ConditionWithAccess>>,
    /// Map from system set to node id
    ids: HashMap<InternedSystemSet, SystemSetKey>,
}

impl SystemSets {
    fn get_or_add_set(&mut self, set: InternedSystemSet) -> SystemSetKey {
        *self.ids.entry(set).or_insert_with(|| {
            let key = self.sets.insert(SystemSetNode::new(set));
            self.conditions.insert(key, Vec::new());
            key