Path: blob/main/crates/bevy_ecs/src/system/schedule_system.rs
9394 views
use bevy_utils::prelude::DebugName;12use crate::{3change_detection::{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> {68// SAFETY: Upheld by caller69unsafe { self.system.run_unsafe(&mut self.value, world) }70}7172#[cfg(feature = "hotpatching")]73#[inline]74fn refresh_hotpatch(&mut self) {75self.system.refresh_hotpatch();76}7778fn apply_deferred(&mut self, world: &mut World) {79self.system.apply_deferred(world);80}8182fn queue_deferred(&mut self, world: DeferredWorld) {83self.system.queue_deferred(world);84}8586unsafe fn validate_param_unsafe(87&mut self,88world: UnsafeWorldCell,89) -> Result<(), SystemParamValidationError> {90// SAFETY: Upheld by caller91unsafe { self.system.validate_param_unsafe(world) }92}9394fn initialize(&mut self, world: &mut World) -> FilteredAccessSet {95self.system.initialize(world)96}9798fn check_change_tick(&mut self, check: CheckChangeTicks) {99self.system.check_change_tick(check);100}101102fn get_last_run(&self) -> Tick {103self.system.get_last_run()104}105106fn set_last_run(&mut self, last_run: Tick) {107self.system.set_last_run(last_run);108}109}110111/// Constructed in [`IntoSystem::with_input_from`].112pub struct WithInputFromWrapper<S, T> {113system: S,114value: Option<T>,115}116117impl<S, T> WithInputFromWrapper<S, T>118where119for<'i> S: System<In: SystemInput<Inner<'i> = &'i mut T>>,120T: Send + Sync + 'static,121{122/// Wraps the given system.123pub fn new<M>(system: impl IntoSystem<S::In, S::Out, M, System = S>) -> Self {124Self {125system: IntoSystem::into_system(system),126value: None,127}128}129130/// Returns a reference to the input value, if it has been initialized.131pub fn value(&self) -> Option<&T> {132self.value.as_ref()133}134135/// Returns a mutable reference to the input value, if it has been initialized.136pub fn value_mut(&mut self) -> Option<&mut T> {137self.value.as_mut()138}139}140141impl<S, T> System for WithInputFromWrapper<S, T>142where143for<'i> S: System<In: SystemInput<Inner<'i> = &'i mut T>>,144T: FromWorld + Send + Sync + 'static,145{146type In = ();147type Out = S::Out;148149fn name(&self) -> DebugName {150self.system.name()151}152153#[inline]154fn flags(&self) -> SystemStateFlags {155self.system.flags()156}157158unsafe fn run_unsafe(159&mut self,160_input: SystemIn<'_, Self>,161world: UnsafeWorldCell,162) -> Result<Self::Out, RunSystemError> {163let value = self164.value165.as_mut()166.expect("System input value was not found. Did you forget to initialize the system before running it?");167// SAFETY: Upheld by caller168unsafe { self.system.run_unsafe(value, world) }169}170171#[cfg(feature = "hotpatching")]172#[inline]173fn refresh_hotpatch(&mut self) {174self.system.refresh_hotpatch();175}176177fn apply_deferred(&mut self, world: &mut World) {178self.system.apply_deferred(world);179}180181fn queue_deferred(&mut self, world: DeferredWorld) {182self.system.queue_deferred(world);183}184185unsafe fn validate_param_unsafe(186&mut self,187world: UnsafeWorldCell,188) -> Result<(), SystemParamValidationError> {189// SAFETY: Upheld by caller190unsafe { self.system.validate_param_unsafe(world) }191}192193fn initialize(&mut self, world: &mut World) -> FilteredAccessSet {194if self.value.is_none() {195self.value = Some(T::from_world(world));196}197self.system.initialize(world)198}199200fn check_change_tick(&mut self, check: CheckChangeTicks) {201self.system.check_change_tick(check);202}203204fn get_last_run(&self) -> Tick {205self.system.get_last_run()206}207208fn set_last_run(&mut self, last_run: Tick) {209self.system.set_last_run(last_run);210}211}212213/// Type alias for a `BoxedSystem` that a `Schedule` can store.214pub type ScheduleSystem = BoxedSystem<(), ()>;215216217