Skip to content

Commit ac43d5c

Browse files
authored
Convert to fallible system in IntoSystemConfigs (#17051)
# Objective - #16589 added an enum to switch between fallible and infallible system. This branching should be unnecessary if we wrap infallible systems in a function to return `Ok(())`. ## Solution - Create a wrapper system for `System<(), ()>`s that returns `Ok` on the call to `run` and `run_unsafe`. The wrapper should compile out, but I haven't checked. - I removed the `impl IntoSystemConfigs for BoxedSystem<(), ()>` as I couldn't figure out a way to keep the impl without double boxing. ## Testing - ran `many_foxes` example to check if it still runs. ## Migration Guide - `IntoSystemConfigs` has been removed for `BoxedSystem<(), ()>`. Either use `InfallibleSystemWrapper` before boxing or make your system return `bevy::ecs::prelude::Result`.
1 parent 33afd38 commit ac43d5c

File tree

8 files changed

+74
-140
lines changed

8 files changed

+74
-140
lines changed

crates/bevy_ecs/src/schedule/config.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
set::{InternedSystemSet, IntoSystemSet, SystemSet},
1010
Chain,
1111
},
12-
system::{BoxedSystem, IntoSystem, ScheduleSystem, System},
12+
system::{BoxedSystem, InfallibleSystemWrapper, IntoSystem, ScheduleSystem, System},
1313
};
1414

1515
fn new_condition<M>(condition: impl Condition<M>) -> BoxedCondition {
@@ -519,6 +519,7 @@ impl IntoSystemConfigs<()> for SystemConfigs {
519519
}
520520
}
521521

522+
/// Marker component to allow for conflicting implementations of [`IntoSystemConfigs`]
522523
#[doc(hidden)]
523524
pub struct Infallible;
524525

@@ -527,17 +528,12 @@ where
527528
F: IntoSystem<(), (), Marker>,
528529
{
529530
fn into_configs(self) -> SystemConfigs {
530-
let boxed_system = Box::new(IntoSystem::into_system(self));
531-
SystemConfigs::new_system(ScheduleSystem::Infallible(boxed_system))
532-
}
533-
}
534-
535-
impl IntoSystemConfigs<()> for BoxedSystem<(), ()> {
536-
fn into_configs(self) -> SystemConfigs {
537-
SystemConfigs::new_system(ScheduleSystem::Infallible(self))
531+
let wrapper = InfallibleSystemWrapper::new(IntoSystem::into_system(self));
532+
SystemConfigs::new_system(Box::new(wrapper))
538533
}
539534
}
540535

536+
/// Marker component to allow for conflicting implementations of [`IntoSystemConfigs`]
541537
#[doc(hidden)]
542538
pub struct Fallible;
543539

@@ -547,13 +543,13 @@ where
547543
{
548544
fn into_configs(self) -> SystemConfigs {
549545
let boxed_system = Box::new(IntoSystem::into_system(self));
550-
SystemConfigs::new_system(ScheduleSystem::Fallible(boxed_system))
546+
SystemConfigs::new_system(boxed_system)
551547
}
552548
}
553549

554550
impl IntoSystemConfigs<()> for BoxedSystem<(), Result> {
555551
fn into_configs(self) -> SystemConfigs {
556-
SystemConfigs::new_system(ScheduleSystem::Fallible(self))
552+
SystemConfigs::new_system(self)
557553
}
558554
}
559555

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
component::{ComponentId, Tick},
1919
prelude::{IntoSystemSet, SystemSet},
2020
query::Access,
21+
result::Result,
2122
schedule::{BoxedCondition, InternedSystemSet, NodeId, SystemTypeSet},
2223
system::{ScheduleSystem, System, SystemIn},
2324
world::{unsafe_world_cell::UnsafeWorldCell, DeferredWorld, World},
@@ -158,7 +159,7 @@ pub(super) fn is_apply_deferred(system: &ScheduleSystem) -> bool {
158159

159160
impl System for ApplyDeferred {
160161
type In = ();
161-
type Out = ();
162+
type Out = Result<()>;
162163

163164
fn name(&self) -> Cow<'static, str> {
164165
Cow::Borrowed("bevy_ecs::apply_deferred")
@@ -203,11 +204,13 @@ impl System for ApplyDeferred {
203204
) -> Self::Out {
204205
// This system does nothing on its own. The executor will apply deferred
205206
// commands from other systems instead of running this system.
207+
Ok(())
206208
}
207209

208210
fn run(&mut self, _input: SystemIn<'_, Self>, _world: &mut World) -> Self::Out {
209211
// This system does nothing on its own. The executor will apply deferred
210212
// commands from other systems instead of running this system.
213+
Ok(())
211214
}
212215

213216
fn apply_deferred(&mut self, _world: &mut World) {}
@@ -259,7 +262,7 @@ mod __rust_begin_short_backtrace {
259262

260263
use crate::{
261264
result::Result,
262-
system::{ReadOnlySystem, ScheduleSystem, System},
265+
system::{ReadOnlySystem, ScheduleSystem},
263266
world::{unsafe_world_cell::UnsafeWorldCell, World},
264267
};
265268

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
prelude::Resource,
2121
query::Access,
2222
schedule::{is_apply_deferred, BoxedCondition, ExecutorKind, SystemExecutor, SystemSchedule},
23-
system::{ScheduleSystem, System},
23+
system::ScheduleSystem,
2424
world::{unsafe_world_cell::UnsafeWorldCell, World},
2525
};
2626

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
schedule::{
88
executor::is_apply_deferred, BoxedCondition, ExecutorKind, SystemExecutor, SystemSchedule,
99
},
10-
system::System,
1110
world::World,
1211
};
1312

@@ -136,7 +135,7 @@ impl SystemExecutor for SimpleExecutor {
136135

137136
impl SimpleExecutor {
138137
/// Creates a new simple executor for use in a [`Schedule`](crate::schedule::Schedule).
139-
/// This calls each system in order and immediately calls [`System::apply_deferred`].
138+
/// This calls each system in order and immediately calls [`System::apply_deferred`](crate::system::System).
140139
pub const fn new() -> Self {
141140
Self {
142141
evaluated_sets: FixedBitSet::new(),

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use tracing::info_span;
55

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

crates/bevy_ecs/src/schedule/schedule.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
prelude::Component,
2222
result::Result,
2323
schedule::*,
24-
system::{IntoSystem, Resource, ScheduleSystem, System},
24+
system::{IntoSystem, Resource, ScheduleSystem},
2525
world::World,
2626
};
2727

@@ -1053,7 +1053,7 @@ impl ScheduleGraph {
10531053
Ok(())
10541054
}
10551055

1056-
/// Initializes any newly-added systems and conditions by calling [`System::initialize`]
1056+
/// Initializes any newly-added systems and conditions by calling [`System::initialize`](crate::system::System)
10571057
pub fn initialize(&mut self, world: &mut World) {
10581058
for (id, i) in self.uninit.drain(..) {
10591059
match id {
@@ -1200,8 +1200,8 @@ impl ScheduleGraph {
12001200
let id = NodeId::System(self.systems.len());
12011201

12021202
self.systems
1203-
.push(SystemNode::new(ScheduleSystem::Infallible(Box::new(
1204-
IntoSystem::into_system(ApplyDeferred),
1203+
.push(SystemNode::new(Box::new(IntoSystem::into_system(
1204+
ApplyDeferred,
12051205
))));
12061206
self.system_conditions.push(Vec::new());
12071207

crates/bevy_ecs/src/schedule/stepping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
schedule::{InternedScheduleLabel, NodeId, Schedule, ScheduleLabel},
3-
system::{IntoSystem, ResMut, Resource, System},
3+
system::{IntoSystem, ResMut, Resource},
44
};
55
use alloc::vec::Vec;
66
use bevy_utils::{HashMap, TypeIdMap};

0 commit comments

Comments
 (0)