Skip to content

Commit 0bd22bb

Browse files
committed
init
1 parent c148665 commit 0bd22bb

File tree

9 files changed

+126
-21
lines changed

9 files changed

+126
-21
lines changed

crates/bevy_ecs/src/schedule/executor/mod.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,31 @@ mod __rust_begin_short_backtrace {
180180
}
181181
}
182182

183-
#[macro_export]
184-
/// Emits a warning about system being skipped.
185-
macro_rules! warn_system_skipped {
186-
($ty:literal, $sys:expr) => {
183+
/// Warnings for systems skipped due to invalid system params.
184+
pub mod validity_warning {
185+
/// Warning for systems.
186+
pub fn system(name: &str) {
187187
bevy_utils::tracing::warn!(
188-
"{} {} was skipped due to inaccessible system parameters.",
189-
$ty,
190-
$sys
188+
"System was skipped due to inaccessible system parameters: {}",
189+
name
191190
)
192-
};
191+
}
192+
193+
/// Warning for system conditions.
194+
pub fn condition(name: &str) {
195+
bevy_utils::tracing::warn!(
196+
"Condition was skipped due to inaccessible system parameters: {}",
197+
name
198+
)
199+
}
200+
201+
/// Warning for observers.
202+
pub fn observer(name: &str) {
203+
bevy_utils::tracing::warn!(
204+
"Observer was skipped due to inaccessible system parameters: {}",
205+
name
206+
)
207+
}
193208
}
194209

195210
#[cfg(test)]

crates/bevy_ecs/src/schedule/executor/multi_threaded.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ use crate::{
1717
archetype::ArchetypeComponentId,
1818
prelude::Resource,
1919
query::Access,
20-
schedule::{is_apply_deferred, BoxedCondition, ExecutorKind, SystemExecutor, SystemSchedule},
20+
schedule::{
21+
is_apply_deferred, validity_warning, BoxedCondition, ExecutorKind, SystemExecutor,
22+
SystemSchedule,
23+
},
2124
system::BoxedSystem,
22-
warn_system_skipped,
2325
world::{unsafe_world_cell::UnsafeWorldCell, World},
2426
};
2527

@@ -524,7 +526,7 @@ impl ExecutorState {
524526
unsafe fn should_run(
525527
&mut self,
526528
system_index: usize,
527-
system: &BoxedSystem,
529+
system: &mut BoxedSystem,
528530
conditions: &mut Conditions,
529531
world: UnsafeWorldCell,
530532
) -> bool {
@@ -575,7 +577,9 @@ impl ExecutorState {
575577
// - `update_archetype_component_access` has been called for system.
576578
let valid_params = unsafe { system.validate_param_unsafe(world) };
577579
if !valid_params {
578-
warn_system_skipped!("System", system.name());
580+
if system.update_validity_warning() {
581+
validity_warning::system(&system.name());
582+
}
579583
self.skipped_systems.insert(system_index);
580584
}
581585
should_run &= valid_params;
@@ -751,7 +755,9 @@ unsafe fn evaluate_and_fold_conditions(
751755
// required by the condition.
752756
// - `update_archetype_component_access` has been called for condition.
753757
if !unsafe { condition.validate_param_unsafe(world) } {
754-
warn_system_skipped!("Condition", condition.name());
758+
if condition.update_validity_warning() {
759+
validity_warning::condition(&condition.name());
760+
}
755761
return false;
756762
}
757763
// SAFETY:

crates/bevy_ecs/src/schedule/executor/simple.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ use crate::{
77
schedule::{
88
executor::is_apply_deferred, BoxedCondition, ExecutorKind, SystemExecutor, SystemSchedule,
99
},
10-
warn_system_skipped,
1110
world::World,
1211
};
1312

14-
use super::__rust_begin_short_backtrace;
13+
use super::{__rust_begin_short_backtrace, validity_warning};
1514

1615
/// A variant of [`SingleThreadedExecutor`](crate::schedule::SingleThreadedExecutor) that calls
1716
/// [`apply_deferred`](crate::system::System::apply_deferred) immediately after running each system.
@@ -84,7 +83,9 @@ impl SystemExecutor for SimpleExecutor {
8483
if should_run {
8584
let valid_params = system.validate_param(world);
8685
if !valid_params {
87-
warn_system_skipped!("System", system.name());
86+
if system.update_validity_warning() {
87+
validity_warning::system(&system.name());
88+
}
8889
}
8990
should_run &= valid_params;
9091
}
@@ -139,7 +140,9 @@ fn evaluate_and_fold_conditions(conditions: &mut [BoxedCondition], world: &mut W
139140
.iter_mut()
140141
.map(|condition| {
141142
if !condition.validate_param(world) {
142-
warn_system_skipped!("Condition", condition.name());
143+
if condition.update_validity_warning() {
144+
validity_warning::condition(&condition.name());
145+
}
143146
return false;
144147
}
145148
__rust_begin_short_backtrace::readonly_run(&mut **condition, world)

crates/bevy_ecs/src/schedule/executor/single_threaded.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ use fixedbitset::FixedBitSet;
55

66
use crate::{
77
schedule::{is_apply_deferred, BoxedCondition, ExecutorKind, SystemExecutor, SystemSchedule},
8-
warn_system_skipped,
98
world::World,
109
};
1110

12-
use super::__rust_begin_short_backtrace;
11+
use super::{__rust_begin_short_backtrace, validity_warning};
1312

1413
/// Runs the schedule using a single thread.
1514
///
@@ -90,7 +89,9 @@ impl SystemExecutor for SingleThreadedExecutor {
9089
if should_run {
9190
let valid_params = system.validate_param(world);
9291
if !valid_params {
93-
warn_system_skipped!("System", system.name());
92+
if system.update_validity_warning() {
93+
validity_warning::system(&system.name());
94+
}
9495
}
9596
should_run &= valid_params;
9697
}
@@ -171,7 +172,9 @@ fn evaluate_and_fold_conditions(conditions: &mut [BoxedCondition], world: &mut W
171172
.iter_mut()
172173
.map(|condition| {
173174
if !condition.validate_param(world) {
174-
warn_system_skipped!("Condition", condition.name());
175+
if condition.update_validity_warning() {
176+
validity_warning::condition(&condition.name());
177+
}
175178
return false;
176179
}
177180
__rust_begin_short_backtrace::readonly_run(&mut **condition, world)

crates/bevy_ecs/src/system/adapter_system.rs

+5
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ where
183183
self.system.validate_param_unsafe(world)
184184
}
185185

186+
#[inline]
187+
fn update_validity_warning(&mut self) -> bool {
188+
self.system.update_validity_warning()
189+
}
190+
186191
fn initialize(&mut self, world: &mut crate::prelude::World) {
187192
self.system.initialize(world);
188193
}

crates/bevy_ecs/src/system/combinator.rs

+14
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ where
216216
self.a.validate_param_unsafe(world) && self.b.validate_param_unsafe(world)
217217
}
218218

219+
#[inline]
220+
fn update_validity_warning(&mut self) -> bool {
221+
// Emit a warning if any of the two systems want it.
222+
// No short-circuiting so all warning states get updated.
223+
self.a.update_validity_warning() | self.b.update_validity_warning()
224+
}
225+
219226
fn initialize(&mut self, world: &mut World) {
220227
self.a.initialize(world);
221228
self.b.initialize(world);
@@ -438,6 +445,13 @@ where
438445
self.a.validate_param(world) && self.b.validate_param(world)
439446
}
440447

448+
#[inline]
449+
fn update_validity_warning(&mut self) -> bool {
450+
// Emit a warning if any of the two systems want it.
451+
// No short-circuiting so all warning states get updated.
452+
self.a.update_validity_warning() | self.b.update_validity_warning()
453+
}
454+
441455
fn initialize(&mut self, world: &mut World) {
442456
self.a.initialize(world);
443457
self.b.initialize(world);

crates/bevy_ecs/src/system/exclusive_function_system.rs

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ where
154154
true
155155
}
156156

157+
#[inline]
158+
fn update_validity_warning(&mut self) -> bool {
159+
self.system_meta.update_validity_warning()
160+
}
161+
157162
#[inline]
158163
fn initialize(&mut self, world: &mut World) {
159164
self.system_meta.last_run = world.change_tick().relative_to(Tick::MAX);

crates/bevy_ecs/src/system/function_system.rs

+49
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct SystemMeta {
4343
is_send: bool,
4444
has_deferred: bool,
4545
pub(crate) last_run: Tick,
46+
validity_warning: ValidityWarning,
4647
#[cfg(feature = "trace")]
4748
pub(crate) system_span: Span,
4849
#[cfg(feature = "trace")]
@@ -59,6 +60,7 @@ impl SystemMeta {
5960
is_send: true,
6061
has_deferred: false,
6162
last_run: Tick::new(0),
63+
validity_warning: ValidityWarning::Once,
6264
#[cfg(feature = "trace")]
6365
system_span: info_span!("system", name = name),
6466
#[cfg(feature = "trace")]
@@ -75,6 +77,7 @@ impl SystemMeta {
7577
/// Sets the name of of this system.
7678
///
7779
/// Useful to give closure systems more readable and unique names for debugging and tracing.
80+
#[inline]
7881
pub fn set_name(&mut self, new_name: impl Into<Cow<'static, str>>) {
7982
let new_name: Cow<'static, str> = new_name.into();
8083
#[cfg(feature = "trace")]
@@ -108,9 +111,50 @@ impl SystemMeta {
108111

109112
/// Marks the system as having deferred buffers like [`Commands`](`super::Commands`)
110113
/// This lets the scheduler insert [`apply_deferred`](`crate::prelude::apply_deferred`) systems automatically.
114+
#[inline]
111115
pub fn set_has_deferred(&mut self) {
112116
self.has_deferred = true;
113117
}
118+
119+
/// Call after [`System::validate_param`] fails.
120+
/// Updates the validity warning.
121+
/// The return value tells whether a warning should be emitted or not.
122+
#[inline]
123+
pub(crate) fn update_validity_warning(&mut self) -> bool {
124+
self.validity_warning.update()
125+
}
126+
127+
/// Configures the validity warning.
128+
#[inline]
129+
pub fn set_validity_warning(&mut self, validity_warning: ValidityWarning) {
130+
self.validity_warning = validity_warning;
131+
}
132+
}
133+
134+
/// State machine for emitting warnings when [system params are invalid](System::validate_param).
135+
#[derive(Clone, Copy)]
136+
pub enum ValidityWarning {
137+
/// No warning should ever be emitted.
138+
Never,
139+
/// The warning will be emitted once and status will update to [`Self::Never`].
140+
Once,
141+
/// The warning will be emitted every time.
142+
Always,
143+
}
144+
145+
impl ValidityWarning {
146+
/// Updates the validity warning.
147+
/// The return value tells whether a warning should be emitted or not.
148+
#[inline]
149+
pub fn update(&mut self) -> bool {
150+
let (next, should_warn) = match self {
151+
Self::Never => (Self::Never, false),
152+
Self::Once => (Self::Never, true),
153+
Self::Always => (Self::Always, true),
154+
};
155+
*self = next;
156+
should_warn
157+
}
114158
}
115159

116160
// TODO: Actually use this in FunctionSystem. We should probably only do this once Systems are constructed using a World reference
@@ -662,6 +706,11 @@ where
662706
F::Param::validate_param(param_state, &self.system_meta, world)
663707
}
664708

709+
#[inline]
710+
fn update_validity_warning(&mut self) -> bool {
711+
self.system_meta.update_validity_warning()
712+
}
713+
665714
#[inline]
666715
fn initialize(&mut self, world: &mut World) {
667716
if let Some(id) = self.world_id {

crates/bevy_ecs/src/system/system.rs

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ pub trait System: Send + Sync + 'static {
129129
unsafe { self.validate_param_unsafe(world_cell) }
130130
}
131131

132+
/// Call after [`System::validate_param`] fails.
133+
/// Updates the validity warning.
134+
/// The return value tells whether a warning should be emitted or not.
135+
fn update_validity_warning(&mut self) -> bool;
136+
132137
/// Initialize the system.
133138
fn initialize(&mut self, _world: &mut World);
134139

0 commit comments

Comments
 (0)