Path: blob/main/crates/bevy_ecs/src/system/schedule_system.rs
6604 views
use bevy_utils::prelude::DebugName;12use crate::{3component::{CheckChangeTicks, Tick},4error::Result,5query::FilteredAccessSet,6system::{input::SystemIn, BoxedSystem, RunSystemError, System, SystemInput},7world::{unsafe_world_cell::UnsafeWorldCell, DeferredWorld, FromWorld, World},8};910use super::{IntoSystem, SystemParamValidationError, SystemStateFlags};1112/// See [`IntoSystem::with_input`] for details.13pub struct WithInputWrapper<S, T>14where15for<'i> S: System<In: SystemInput<Inner<'i> = &'i mut T>>,16T: Send + Sync + 'static,17{18system: S,19value: T,20}2122impl<S, T> WithInputWrapper<S, T>23where24for<'i> S: System<In: SystemInput<Inner<'i> = &'i mut T>>,25T: Send + Sync + 'static,26{27/// Wraps the given system with the given input value.28pub fn new<M>(system: impl IntoSystem<S::In, S::Out, M, System = S>, value: T) -> Self {29Self {30system: IntoSystem::into_system(system),31value,32}33}3435/// Returns a reference to the input value.36pub fn value(&self) -> &T {37&self.value38}3940/// Returns a mutable reference to the input value.41pub fn value_mut(&mut self) -> &mut T {42&mut self.value43}44}4546impl<S, T> System for WithInputWrapper<S, T>47where48for<'i> S: System<In: SystemInput<Inner<'i> = &'i mut T>>,49T: Send + Sync + 'static,50{51type In = ();52type Out = S::Out;5354fn name(&self) -> DebugName {55self.system.name()56}5758#[inline]59fn flags(&self) -> SystemStateFlags {60self.system.flags()61}6263unsafe fn run_unsafe(64&mut self,65_input: SystemIn<'_, Self>,66world: UnsafeWorldCell,67) -> Result<Self::Out, RunSystemError> {68self.system.run_unsafe(&mut self.value, world)69}7071#[cfg(feature = "hotpatching")]72#[inline]73fn refresh_hotpatch(&mut self) {74self.system.refresh_hotpatch();75}7677fn apply_deferred(&mut self, world: &mut World) {78self.system.apply_deferred(world);79}8081fn queue_deferred(&mut self, world: DeferredWorld) {82self.system.queue_deferred(world);83}8485unsafe fn validate_param_unsafe(86&mut self,87world: UnsafeWorldCell,88) -> Result<(), SystemParamValidationError> {89self.system.validate_param_unsafe(world)90}9192fn initialize(&mut self, world: &mut World) -> FilteredAccessSet {93self.system.initialize(world)94}9596fn check_change_tick(&mut self, check: CheckChangeTicks) {97self.system.check_change_tick(check);98}99100fn get_last_run(&self) -> Tick {101self.system.get_last_run()102}103104fn set_last_run(&mut self, last_run: Tick) {105self.system.set_last_run(last_run);106}107}108109/// Constructed in [`IntoSystem::with_input_from`].110pub struct WithInputFromWrapper<S, T> {111system: S,112value: Option<T>,113}114115impl<S, T> WithInputFromWrapper<S, T>116where117for<'i> S: System<In: SystemInput<Inner<'i> = &'i mut T>>,118T: Send + Sync + 'static,119{120/// Wraps the given system.121pub fn new<M>(system: impl IntoSystem<S::In, S::Out, M, System = S>) -> Self {122Self {123system: IntoSystem::into_system(system),124value: None,125}126}127128/// Returns a reference to the input value, if it has been initialized.129pub fn value(&self) -> Option<&T> {130self.value.as_ref()131}132133/// Returns a mutable reference to the input value, if it has been initialized.134pub fn value_mut(&mut self) -> Option<&mut T> {135self.value.as_mut()136}137}138139impl<S, T> System for WithInputFromWrapper<S, T>140where141for<'i> S: System<In: SystemInput<Inner<'i> = &'i mut T>>,142T: FromWorld + Send + Sync + 'static,143{144type In = ();145type Out = S::Out;146147fn name(&self) -> DebugName {148self.system.name()149}150151#[inline]152fn flags(&self) -> SystemStateFlags {153self.system.flags()154}155156unsafe fn run_unsafe(157&mut self,158_input: SystemIn<'_, Self>,159world: UnsafeWorldCell,160) -> Result<Self::Out, RunSystemError> {161let value = self162.value163.as_mut()164.expect("System input value was not found. Did you forget to initialize the system before running it?");165self.system.run_unsafe(value, world)166}167168#[cfg(feature = "hotpatching")]169#[inline]170fn refresh_hotpatch(&mut self) {171self.system.refresh_hotpatch();172}173174fn apply_deferred(&mut self, world: &mut World) {175self.system.apply_deferred(world);176}177178fn queue_deferred(&mut self, world: DeferredWorld) {179self.system.queue_deferred(world);180}181182unsafe fn validate_param_unsafe(183&mut self,184world: UnsafeWorldCell,185) -> Result<(), SystemParamValidationError> {186self.system.validate_param_unsafe(world)187}188189fn initialize(&mut self, world: &mut World) -> FilteredAccessSet {190if self.value.is_none() {191self.value = Some(T::from_world(world));192}193self.system.initialize(world)194}195196fn check_change_tick(&mut self, check: CheckChangeTicks) {197self.system.check_change_tick(check);198}199200fn get_last_run(&self) -> Tick {201self.system.get_last_run()202}203204fn set_last_run(&mut self, last_run: Tick) {205self.system.set_last_run(last_run);206}207}208209/// Type alias for a `BoxedSystem` that a `Schedule` can store.210pub type ScheduleSystem = BoxedSystem<(), ()>;211212213