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

Code before:
        }

        // 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 {
            panic!("`RunSystemStack` should never be removed during the execution of a one-shot system.");
        };
        sys_stack.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,
            });
        }


Review comment (refactor):
I think you can simplify the "or panic" logic with `expect` (here and below).  

```suggestion
        self.get_resource_mut::<RunSystemStack>()
            .expect("`RunSystemStack` should never be removed during the execution of a one-shot system.")
            .insert(id.entity);
```

Code after:
    ///
    /// # 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 {
            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));