Skip to content

Commit 4b51c21

Browse files
Rename When system param to If (#20325)
# Objective Regardless of where we land on #19489, `When` as a system param wrapper is a touch verbose. In many cases, users may find themselves liberally applying `When` to their fallible system params. I believe that `If` maintains the same semantics in a smaller and more readable package, and therefore should replace `When`. ## Showcase `When`: ```rs fn fallible_params(player: When<Single<&mut Velocity, With<Player>>>, gravity: When<Res<Gravity>>) {} ``` `If`: ```rs fn fallible_params(player: If<Single<&mut Velocity, With<Player>>>, gravity: If<Res<Gravity>>) {} ```
1 parent f576aca commit 4b51c21

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

crates/bevy_ecs/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ pub mod prelude {
9999
},
100100
spawn::{Spawn, SpawnIter, SpawnRelated, SpawnWith, WithOneRelated, WithRelated},
101101
system::{
102-
Command, Commands, Deferred, EntityCommand, EntityCommands, In, InMut, InRef,
102+
Command, Commands, Deferred, EntityCommand, EntityCommands, If, In, InMut, InRef,
103103
IntoSystem, Local, NonSend, NonSendMut, ParamSet, Populated, Query, ReadOnlySystem,
104104
Res, ResMut, Single, System, SystemIn, SystemInput, SystemParamBuilder,
105-
SystemParamFunction, When,
105+
SystemParamFunction,
106106
},
107107
world::{
108108
EntityMut, EntityRef, EntityWorldMut, FilteredResources, FilteredResourcesMut,

crates/bevy_ecs/src/system/builder.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::{
77
query::{QueryData, QueryFilter, QueryState},
88
resource::Resource,
99
system::{
10-
DynSystemParam, DynSystemParamState, Local, ParamSet, Query, SystemParam,
11-
SystemParamValidationError, When,
10+
DynSystemParam, DynSystemParamState, If, Local, ParamSet, Query, SystemParam,
11+
SystemParamValidationError,
1212
},
1313
world::{
1414
FilteredResources, FilteredResourcesBuilder, FilteredResourcesMut,
@@ -605,15 +605,13 @@ unsafe impl<P: SystemParam, B: SystemParamBuilder<P>>
605605
}
606606
}
607607

608-
/// A [`SystemParamBuilder`] for a [`When`].
608+
/// A [`SystemParamBuilder`] for a [`If`].
609609
#[derive(Clone)]
610-
pub struct WhenBuilder<T>(T);
610+
pub struct IfBuilder<T>(T);
611611

612-
// SAFETY: `WhenBuilder<B>` builds a state that is valid for `P`, and any state valid for `P` is valid for `When<P>`
613-
unsafe impl<P: SystemParam, B: SystemParamBuilder<P>> SystemParamBuilder<When<P>>
614-
for WhenBuilder<B>
615-
{
616-
fn build(self, world: &mut World) -> <When<P> as SystemParam>::State {
612+
// SAFETY: `IfBuilder<B>` builds a state that is valid for `P`, and any state valid for `P` is valid for `If<P>`
613+
unsafe impl<P: SystemParam, B: SystemParamBuilder<P>> SystemParamBuilder<If<P>> for IfBuilder<B> {
614+
fn build(self, world: &mut World) -> <If<P> as SystemParam>::State {
617615
self.0.build(world)
618616
}
619617
}

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,16 +1846,16 @@ unsafe impl<T: ReadOnlySystemParam> ReadOnlySystemParam for Result<T, SystemPara
18461846
/// fn fails_on_missing_resource(res: Res<SomeResource>) {}
18471847
///
18481848
/// // This system will skip without error if `SomeResource` is not present.
1849-
/// fn skips_on_missing_resource(res: When<Res<SomeResource>>) {
1849+
/// fn skips_on_missing_resource(res: If<Res<SomeResource>>) {
18501850
/// // The inner parameter is available using `Deref`
18511851
/// let some_resource: &SomeResource = &res;
18521852
/// }
18531853
/// # bevy_ecs::system::assert_is_system(skips_on_missing_resource);
18541854
/// ```
18551855
#[derive(Debug)]
1856-
pub struct When<T>(pub T);
1856+
pub struct If<T>(pub T);
18571857

1858-
impl<T> When<T> {
1858+
impl<T> If<T> {
18591859
/// Returns the inner `T`.
18601860
///
18611861
/// The inner value is `pub`, so you can also obtain it by destructuring the parameter:
@@ -1864,7 +1864,7 @@ impl<T> When<T> {
18641864
/// # use bevy_ecs::prelude::*;
18651865
/// # #[derive(Resource)]
18661866
/// # struct SomeResource;
1867-
/// fn skips_on_missing_resource(When(res): When<Res<SomeResource>>) {
1867+
/// fn skips_on_missing_resource(If(res): If<Res<SomeResource>>) {
18681868
/// let some_resource: Res<SomeResource> = res;
18691869
/// }
18701870
/// # bevy_ecs::system::assert_is_system(skips_on_missing_resource);
@@ -1874,24 +1874,24 @@ impl<T> When<T> {
18741874
}
18751875
}
18761876

1877-
impl<T> Deref for When<T> {
1877+
impl<T> Deref for If<T> {
18781878
type Target = T;
18791879
fn deref(&self) -> &Self::Target {
18801880
&self.0
18811881
}
18821882
}
18831883

1884-
impl<T> DerefMut for When<T> {
1884+
impl<T> DerefMut for If<T> {
18851885
fn deref_mut(&mut self) -> &mut Self::Target {
18861886
&mut self.0
18871887
}
18881888
}
18891889

18901890
// SAFETY: Delegates to `T`, which ensures the safety requirements are met
1891-
unsafe impl<T: SystemParam> SystemParam for When<T> {
1891+
unsafe impl<T: SystemParam> SystemParam for If<T> {
18921892
type State = T::State;
18931893

1894-
type Item<'world, 'state> = When<T::Item<'world, 'state>>;
1894+
type Item<'world, 'state> = If<T::Item<'world, 'state>>;
18951895

18961896
fn init_state(world: &mut World) -> Self::State {
18971897
T::init_state(world)
@@ -1925,7 +1925,7 @@ unsafe impl<T: SystemParam> SystemParam for When<T> {
19251925
world: UnsafeWorldCell<'world>,
19261926
change_tick: Tick,
19271927
) -> Self::Item<'world, 'state> {
1928-
When(T::get_param(state, system_meta, world, change_tick))
1928+
If(T::get_param(state, system_meta, world, change_tick))
19291929
}
19301930

19311931
fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World) {
@@ -1938,7 +1938,7 @@ unsafe impl<T: SystemParam> SystemParam for When<T> {
19381938
}
19391939

19401940
// SAFETY: Delegates to `T`, which ensures the safety requirements are met
1941-
unsafe impl<T: ReadOnlySystemParam> ReadOnlySystemParam for When<T> {}
1941+
unsafe impl<T: ReadOnlySystemParam> ReadOnlySystemParam for If<T> {}
19421942

19431943
// SAFETY: Registers access for each element of `state`.
19441944
// If any one conflicts, it will panic.
@@ -2784,7 +2784,7 @@ pub struct SystemParamValidationError {
27842784
/// failures in validation should be considered a bug in the user's logic that must be immediately addressed (like [`Res`]).
27852785
///
27862786
/// If `true`, the system should be skipped.
2787-
/// This is set by wrapping the system param in [`When`],
2787+
/// This is set by wrapping the system param in [`If`],
27882788
/// and indicates that the system is intended to only operate in certain application states.
27892789
pub skipped: bool,
27902790

@@ -2848,7 +2848,7 @@ impl Display for SystemParamValidationError {
28482848
self.message
28492849
)?;
28502850
if !self.skipped {
2851-
write!(fmt, "\nIf this is an expected state, wrap the parameter in `Option<T>` and handle `None` when it happens, or wrap the parameter in `When<T>` to skip the system when it happens.")?;
2851+
write!(fmt, "\nIf this is an expected state, wrap the parameter in `Option<T>` and handle `None` when it happens, or wrap the parameter in `If<T>` to skip the system when it happens.")?;
28522852
}
28532853
Ok(())
28542854
}

release-content/migration-guides/system_run_returns_result.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "`System::run` returns `Result`"
33
pull_requests: [19145]
44
---
55

6-
In order to support fallible systems and parameter-based system skipping like `Single` and `When<T>` in more places, `System::run` and related methods now return a `Result` instead of a plain value.
6+
In order to support fallible systems and parameter-based system skipping like `Single` and `If<T>` in more places, `System::run` and related methods now return a `Result` instead of a plain value.
77

88
If you were calling `System::run`, `System::run_unsafe`, `System::run_without_applying_deferred`, or `ReadOnlySystem::run_readonly`, the simplest solution is to `unwrap()` the resulting `Result`.
99
The only case where an infallible system will return `Err` is an invalid parameter, such as a missing resource, and those cases used to panic.

0 commit comments

Comments
 (0)