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

Code before:
        &mut self,
        id: SystemId<I, O>,
        input: I::Inner<'_>,
    ) -> Result<O, RegisteredSystemError<I, O>>
    where
        I: SystemInput + 'static,
        O: 'static,
    {
        self.init_resource::<RunSystemStack>();

        if self.resource::<RunSystemStack>().contains(&id.entity) {
            return Err(RegisteredSystemError::Recursive(id));
        }

        // 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 {
            return Err(RegisteredSystemError::MaybeIncorrectType(id));
        };

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

        let Some(mut sys_stack) = self.get_resource_mut::<RunSystemStack>() else {
            panic!("`RunSystemStack` should never be removed during the execution of a one-shot system.");
        };
        sys_stack.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
            });

        let Some(mut sys_stack) = self.get_resource_mut::<RunSystemStack>() else {

Review comment (bug):
I think this could keep doing `ok_or` and `?` like the old code, instead of an explicit `return`.  You're just changing `Recursive` to `MaybeIncorrectType`, right?

Code after:
    pub fn run_system<O: 'static>(
        &mut self,
        id: SystemId<(), O>,
    ) -> Result<O, RegisteredSystemError<(), O>> {
        self.run_system_with(id, ())
    }

    /// Run a stored chained system by its [`SystemId`], providing an input value.
    /// Before running a system, it must first be registered.
    /// The method [`World::register_system`] stores a given system and returns a [`SystemId`].
    ///
    /// Also runs any queued-up commands.
    ///
    /// # Examples
    ///
    /// ```
    /// # use bevy_ecs::prelude::*;
    /// fn increment(In(increment_by): In<u8>, mut counter: Local<u8>) -> u8 {
    ///   *counter += increment_by;
    ///   *counter
    /// }
    ///
    /// let mut world = World::default();
    /// let counter_one = world.register_system(increment);
    /// 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 {