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

Code before:
            mut system,
        }) = entity.take::<RegisteredSystem<I, O>>()
        else {
            return Err(RegisteredSystemError::MaybeIncorrectType(id));
        };

        // Initialize the system
        if !initialized {
            system.initialize(self);
            initialized = true;
        }

        self.resource_mut::<RunSystemStack>().insert(id.entity);

        let result = system
            .validate_param(self)
            .map_err(|err| RegisteredSystemError::InvalidParams { system: id, err })
            .map(|()| {
                // Wait to run the commands until the system is available again.
                // This is needed so the systems can recursively run themselves.
                let ret = system.run_without_applying_deferred(input, self);
                system.queue_deferred(self.into());
                ret
            });

        self.resource_mut::<RunSystemStack>().remove(&id.entity);

        // Return ownership of system trait object (if entity still exists)
        if let Ok(mut entity) = self.get_entity_mut(id.entity) {
            entity.insert::<RegisteredSystem<I, O>>(RegisteredSystem {
                initialized,
                system,
            });
        }

        // Run any commands enqueued by the system
        self.flush();
        result
    }

    /// Registers a system or returns its cached [`SystemId`].
    ///
    /// If you want to run the system immediately and you don't need its `SystemId`, see
    /// [`World::run_system_cached`].
    ///
    /// The first time this function is called for a particular system, it will register it and
    /// store its [`SystemId`] in a [`CachedSystemId`] resource for later. If you would rather
    /// manage the `SystemId` yourself, or register multiple copies of the same system, use
    /// [`World::register_system`] instead.
    ///
    /// # Limitations

Review comment (suggestion):
This could technically panic if a malicious system finds the `RunSystemStack` resource by ID and removes it.  It might be a little safer like

```suggestion
        if let Some(run_system_stack) = self.get_resource_mut::<RunSystemStack>() {
            run_system_stack.remove(&id.entity);
        }
```

We may even want to do the same thing for `insert`, since a user could register a malicious observer that runs during `take::<RegisteredSystem>`.  

Code after:
    /// let counter_two = world.register_system(increment);
    /// assert_eq!(world.run_system_with(counter_one, 1).unwrap(), 1);
    /// assert_eq!(world.run_system_with(counter_one, 20).unwrap(), 21);
    /// assert_eq!(world.run_system_with(counter_two, 30).unwrap(), 30);
    /// ```
    ///
    /// See [`World::run_system`] for more examples.
    pub fn run_system_with<I, O>(
        &mut self,
        id: SystemId<I, O>,
        input: I::Inner<'_>,
    ) -> Result<O, RegisteredSystemError<I, O>>
    where
        I: SystemInput + 'static,
        O: 'static,
    {
        // Lookup
        let mut entity = self
            .get_entity_mut(id.entity)
            .map_err(|_| RegisteredSystemError::SystemIdNotRegistered(id))?;

        // Take ownership of system trait object
        let Some(RegisteredSystem {
            mut initialized,
            mut system,
        }) = entity.take::<RegisteredSystem<I, O>>()
        else {
            let Some(system_id_marker) = entity.get::<SystemIdMarker>() else {
                return Err(RegisteredSystemError::SystemIdNotRegistered(id));
            };
            if system_id_marker.input_type_id.type_id != TypeId::of::<I>()
                || system_id_marker.output_type_id.type_id != TypeId::of::<O>()
            {
                return Err(RegisteredSystemError::IncorrectType(
                    id,
                    system_id_marker.clone(),
                ));
            }
            return Err(RegisteredSystemError::Recursive(id));
        };

        // Initialize the system
        if !initialized {
            system.initialize(self);
            initialized = true;
        }

        // refresh hotpatches for stored systems
        #[cfg(feature = "hotpatching")]
        if self
            .get_resource_ref::<HotPatchChanges>()